GoogleBigQuery—定义子表,然后使用sql查询该表

jjhzyzn0  于 2021-08-01  发布在  Java
关注(0)|答案(2)|浏览(206)

我有一个包含许多列的表,我想计算每列的唯一值。我知道我能做到

SELECT sho_01, COUNT(*) from sho GROUP BY sho_01 
UNION ALL 
SELECT sho_02, COUNT(*) from sho GROUP BY sho_02 
UNION ALL
....

在这里 sho 是table和table吗 sho_01 ,.... 是单独的列。顺便说一下,这是bigquery,所以他们使用 UNION ALL .
接下来,我想做同样的事情,但是对于 sho ,说 SELECT * FROM sho WHERE id in (1,2,3) . 有没有一种方法可以先创建子表,然后查询子表?像这样的

SELECT * FROM (SELECT * FROM sho WHERE id IN (1,2,3)) AS t1;
SELECT sho_01, COUNT(*) from t1 GROUP BY sho_01 
UNION ALL 
SELECT sho_02, COUNT(*) from t1 GROUP BY sho_02 
UNION ALL
....

谢谢

tkclm6bt

tkclm6bt1#

据推测,这些列都是同一类型的。如果是这样,您可以使用数组来简化此过程:

select el.which, el.val, count(*)
from (select t1.*,
             array[struct('sho_01' as which, sho_01 as val),
                   struct('sho_2', show_02),
                   . . .
                  ] as ar
      from t
     ) t cross join
     unnest(ar) el
group by el.which, el.val;

然后,您可以通过添加 where 合同前条款 group by .

xqnpmsa8

xqnpmsa82#

下面是bigquery标准sql,允许您避免手动键入列名,甚至提前知道列名


# standardSQL

SELECT 
  TRIM(SPLIT(kv, ':')[OFFSET(0)], '"') column, 
  SPLIT(kv, ':')[OFFSET(1)] value,
  COUNT(1) cnt
FROM `project.dataset.table` t,
UNNEST(SPLIT(TRIM(TO_JSON_STRING(t), '{}'))) kv
GROUP BY column, value
-- ORDER BY column, value

相关问题