我正在尝试使用包含子查询的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
有办法做到这一点吗?如果是这样,我们将非常感谢任何帮助。
谢谢
答案 0 :(得分: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