我有一个 BigQuery 表,其中包含一列RECORD类型&模式重复。我必须在Tableau中查询并使用此表。在BigQuery中使用UNNEST或FLATTEN正在执行表的CROSS JOIN,这会影响性能。有没有其他方法可以在 Tableau 中使用此表而不展平它。已发布下面的表架构图像链接。
答案 0 :(得分:1)
有没有其他方法可以使用......?
你不应该害怕UNNEST,因为它“做”了CROSS JOIN
诀窍在于,即使它是交叉连接,但它只是在行内的交叉连接,而是全局到表中的所有行。与此同时,总有办法做不同的事情
因此,下面的示例1 - 使用UNNEST
呈现虚拟示例
然后示例2 - 显示了如何在不使用UNNEST的情况下执行相同操作,而是使用SQL UDF
您没有提供有关您的案例的详细信息,因此下面的内容足以显示“其他”方式
#standardSQL
WITH yourTable AS (
SELECT 1 AS id, ARRAY<STRUCT<details INT64, flag STRING, value STRING, description STRING>>
[(1,'y','a','xxx'),(2,'n','b','yyy'),(3,'y','c','zzz'),(4,'n','d','vvv')] AS type UNION ALL
SELECT 2 AS id, ARRAY<STRUCT<details INT64, flag STRING, value STRING, description STRING>>
[(11,'t','c','xxx'),(21,'n','a','yyy'),(31,'y','c','zzz'),(41,'f','d','vvv')] AS type
)
SELECT id, SUM(t.details) AS details
FROM yourTable, UNNEST(type) AS t
WHERE t.flag = 'y'
GROUP BY id
#standardSQL
CREATE TEMP FUNCTION do_something (
type ARRAY<STRUCT<details INT64, flag STRING, value STRING, description STRING>>
)
RETURNS INT64 AS ((
SELECT SUM(t.details) AS details
FROM UNNEST(type) AS t
WHERE t.flag = 'y'
));
WITH yourTable AS (
SELECT 1 AS id, ARRAY<STRUCT<details INT64, flag STRING, value STRING, description STRING>>
[(1,'y','a','xxx'),(2,'n','b','yyy'),(3,'y','c','zzz'),(4,'n','d','vvv')] AS type UNION ALL
SELECT 2 AS id, ARRAY<STRUCT<details INT64, flag STRING, value STRING, description STRING>>
[(11,'t','c','xxx'),(21,'n','a','yyy'),(31,'y','c','zzz'),(41,'f','d','vvv')] AS type
)
SELECT id, do_something(type) AS details
FROM yourTable