在BigQuery中查询表

时间:2016-08-18 14:30:49

标签: mysql sql json group-by google-bigquery

背景

我有一个包含1列的数据表'数据'其中包含' JSON'在BigQuery中显示如下。

 data    
 {"name":"x","mobile":999,"location":"abc"}
 {"name":"x1","mobile":9991,"location":"abc1"}

现在,我想使用groupby函数:

SELECT
    data
FROM
    table 
GROUP BY 
    json_extract(data,'$.location')

此查询引发错误

  GROUP BY中的

表达式JSON_EXTRACT([data],' $。location')无效

所以,我将查询修改为

SELECT
    data, json_extract(data,'$.location') as l
FROM
    table 
GROUP BY
    l

此查询抛出错误

  

表达式'数据'在GROUP BY列表中不存在

查询

我们如何在group by子句中使用JSON字段?

在使用JSON填充列时,有哪些限制(在查询的上下文中)。

2 个答案:

答案 0 :(得分:1)

您按位置对某些内容进行分组,但是您没有使用data字段的聚合函数,因此编译器不知道要在源上进行选择或汇总的内容。

为了说明我使用group_concat编译此测试查询的示例:

select group_concat(data),location from
(
select * from
(SELECT '{"name":"x","mobile":999,"location":"abc"}' as data,json_extract('{"name":"x","mobile":999,"location":"abc"}','$.location') as location),
(SELECT '{"name":"x","mobile":111,"location":"abc"}' as data,json_extract('{"name":"x","mobile":111,"location":"abc"}','$.location') as location),
(SELECT '{"name":"x1","mobile":9991,"location":"abc1"}' as data,json_extract('{"name":"x1","mobile":9991,"location":"abc1"}','$.location') as location)

) d
group by location

并返回:

+-----+---------------------------------------------------------------------------------------------------+----------+--+
| Row | f0_                                                                                               | location |  |
+-----+---------------------------------------------------------------------------------------------------+----------+--+
| 1   | {"name":"x","mobile":999,"location":"abc"},"{""name"":""x"",""mobile"":111,""location"":""abc""}" | abc      |  |
+-----+---------------------------------------------------------------------------------------------------+----------+--+
| 2   | {"name":"x1","mobile":9991,"location":"abc1"}                                                     | abc1     |  |
+-----+---------------------------------------------------------------------------------------------------+----------+--+

BigQuery's Aggregate Functions documented here

答案 1 :(得分:1)

尝试以下

SELECT location,
  GROUP_CONCAT_UNQUOTED(REPLACE(data, ',"location":"' + location + '"', '')) AS data
FROM (
  SELECT data,
    JSON_EXTRACT_SCALAR(data,'$.location') AS  location,
  FROM YourTable
)
GROUP BY location