如何在 bigquery 中拆分列和分组?

How can i split column and group by in bigquery?

我在 Legacy SQL 中有一个 SQL 代码已经工作,

但在标准中 SQL 是错误的,

得到回复:

 Grouping by expressions of type ARRAY is not allowed

有什么办法可以解决吗?

这是我的 SQL 代码:

  select tag
  from 
  (
    select SPLIT(content_tag, ',') as tag
    from `test.log`
  )
  group by tag

我认为您在查询中遗漏了 [SAFE_OFFSET(1)],这应该有效

SELECT SPLIT(content_tag, ',') [SAFE_OFFSET(1)] AS tag
FROM `test.log`
GROUP BY tag

已编辑格式代码。

我猜你想要这样的东西:

select tag, count(*)
from (select 'a b c' as tags union all
      select 'd c'
     ) `test.log` cross join
     unnest(split(tags, ' ')) tag
group by tag
order by count(*) desc;

这将计算 space 分隔标签列表中的标签数量。

您提供的遗留 SQL 查询将隐式地展平从 SPLIT 函数返回的数组,这就是它起作用的原因。使用标准 SQL,您需要更明确,但是:

select tag
from `test.log`,
  UNNEST(SPLIT(content_tag, ',')) as tag
group by tag