如何在Big Query中基于嵌套标签修复具有Join的Query?

时间:2019-01-15 12:21:00

标签: sql google-bigquery

我想从Google大查询中的2个来源中联接表。我一直将成功列设为0。正在显示总计。我加入错了吗?

SELECT s1.date, s1.t, s1.success, s2.total FROM 
(SELECT DATE(r.timestamp) AS date,  a.t, SUM(CASE WHEN (SELECT num_value FROM UNNEST(r.labels) WHERE key="xyz")=100 AND (SELECT CAST(SPLIT(str_value, 's')[OFFSET(0)] AS NUMERIC) FROM UNNEST(r.labels) WHERE key="abc")<6 THEN 1 ELSE 0 END) AS success FROM `table` AS a
INNER JOIN `table` AS r 
ON (SELECT str_value FROM UNNEST(a.labels) WHERE key="first") = (SELECT str_value FROM UNNEST(r.labels) WHERE key="second")  
GROUP BY DATE(r.timestamp), a.t ) s1 ,
(SELECT DATE(r.timestamp) AS date, a.t, COUNT(*) AS total FROM `table` AS a
INNER JOIN `table` AS r 
ON (SELECT str_value FROM UNNEST(a.labels) WHERE key="first") = (SELECT str_value FROM UNNEST(r.labels) WHERE key="second") 
GROUP BY DATE(r.timestamp), a.t) s2 
WHERE s1.date=s2.date AND s1.t=s2.t
ORDER BY s1.date 

0 个答案:

没有答案