BigQuery:按日期将子选择合并为一行
Posted
技术标签:
【中文标题】BigQuery:按日期将子选择合并为一行【英文标题】:BigQuery: Consolidate subselect into one row by date 【发布时间】:2015-07-01 21:23:51 【问题描述】:我正在尝试让包含子查询的 BigQuery 查询结果返回一行而不是两行。我正在查询日志文件,所以我需要的所有数据都在同一个字段中。字段中的数据示例如下:
/?cv=p15.0.9350&ctyp=sp&bits=64&os_bits=64&hl=fr&hl=fr&os=win&osv=6.2
我一直在处理的查询如下:
SELECT day, Win, Mac
FROM
(SELECT DATE(metadata.timestamp) AS day, COUNT(DISTINCT protoPayload.resource) AS Win
FROM [su_dashboard_streamed_logs.appengine_googleapis_com_request_log_20150424]
WHERE protoPayload.resource CONTAINS 'ctyp=sp'
GROUP BY day),
(SELECT DATE(metadata.timestamp) AS day, COUNT(DISTINCT protoPayload.resource) AS Mac
FROM [request_log_20150424]
WHERE protoPayload.resource CONTAINS 'ctyp=sm'
GROUP BY day)
ORDER BY day
目前上面的查询返回:
Row day Win Mac
1 2015-04-24 160516 null
2 2015-04-24 null 109547
我希望结果是:
Row day Win Mac
1 2015-04-24 160516 109547
有没有办法做到这一点?如果是这样,任何帮助将不胜感激。
谢谢
【问题讨论】:
【参考方案1】:你想JOIN
两个子选择而不是联合它们。在 BigQuery 中,a comma within a FROM
clause indicates a union:
注意:与许多其他基于 SQL 的系统不同,BigQuery 使用逗号语法来表示表联合,而不是联合。
如果您在日期字段上JOIN
,那么您可以将两行压缩为一行,如下所示:
SELECT table_1.day as day, table_1.Win as Win, table_2.Mac AS Mac
FROM
(SELECT DATE(metadata.timestamp) AS day, COUNT(DISTINCT protoPayload.resource) AS Win
FROM [su_dashboard_streamed_logs.appengine_googleapis_com_request_log_20150424]
WHERE protoPayload.resource CONTAINS 'ctyp=sp'
GROUP BY day) AS table_1
JOIN
(SELECT DATE(metadata.timestamp) AS day, COUNT(DISTINCT protoPayload.resource) AS Mac
FROM [request_log_20150424]
WHERE protoPayload.resource CONTAINS 'ctyp=sm'
GROUP BY day) AS table_2
ON table_1.day = table_2.day
ORDER BY day
【讨论】:
非常感谢丹尼,我已经尝试了一天多的时间。你解决了我的一个主要问题。再次感谢!!以上是关于BigQuery:按日期将子选择合并为一行的主要内容,如果未能解决你的问题,请参考以下文章