【问题标题】:COUNT() AND GROUP BY in BIGQUERYBIGQUERY 中的 COUNT() 和 GROUP BY
【发布时间】:2021-09-30 12:52:31
【问题描述】:

我尝试在 BigQuery 中使用 UNNEST,但输出如下所示

------------------------------------------
|Row |    Name         |   user_id       |
------------------------------------------
| 1  |    STAY         |      1          |
-    -------------------
|    |LOVE YOURSELF    |                 |
-    -------------------
|    |  GREEN BOOK     |                 |
-    -------------------
|    |CRAZY IN LOVE    |                 |
------------------------------------------

我的查询是:

select user_id,
       array(select json_extract_scalar(x, '$.Name') from 
       unnest(json_extract_array(json_col, '$.info.music') x) NAME
from data 

我希望表格如下,因为我想计算听每首歌曲的用户数量。有没有人有什么建议?我真的可以使用上表来计算吗?

------------------------------------------
|Row |    Name         |   user_id       |
------------------------------------------
| 1  |    STAY         |      1          |
------------------------------------------
| 2  |LOVE YOURSELF    |      1          |
------------------------------------------
| 3  |  GREEN BOOK     |      1          |
------------------------------------------
| 4  |CRAZY IN LOVE    |      1          |
------------------------------------------

【问题讨论】:

  • 向我们展示您的查询。
  • select user_id, array(select json_extract_scalar(x, '$.Name') from unnest(json_extract_array(json_col, '$.info.music') x) NAME from data

标签: sql google-bigquery


【解决方案1】:

考虑下面

select name, user_id from (
  select user_id,
    array(select json_extract_scalar(x, '$.Name') from 
    unnest(json_extract_array(json_col, '$.info.music')) x) NAME
  from data
) t, t.name name            

有输出

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2014-05-20
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多