Google Big Query中每日点击次数

时间:2015-05-14 08:27:45

标签: sql google-bigquery

我正在使用Google Big Query来查找每天的点击量。这是我的查询,

SELECT COUNT(*) AS Key, 
       DATE(EventDateUtc) AS Value 
FROM    [myDataSet.myTable] 
WHERE    ..... 
GROUP BY Value 
ORDER BY Value DESC 
LIMIT 1000;

这样可以正常工作,但忽略了0次点击的日期。我想包括这个。我无法在Google Big Query中创建临时表。如何解决这个问题。

经过测试获得错误Field 'day' not found.

SELECT COUNT(*) AS Key, 
       DATE(t.day) AS Value  from (
       select date(date_add(day, i, "DAY")) day
from  (select '2015-05-01 00:00' day) a 
cross join
(select 
   position(
     split(
       rpad('', datediff(CURRENT_TIMESTAMP(),'2015-05-01 00:00')*2, 'a,'))) i 
 from (select NULL)) b

       ) d
left join [sample_data.requests] t on d.day = t.day
GROUP BY Value 
ORDER BY Value DESC 
LIMIT 1000;

1 个答案:

答案 0 :(得分:1)

您可以查询表中存在的数据,查询无法猜测表中缺少的日期。您需要使用编程语言处理此问题,或者可以使用join with a numbers table and generates the dates on the fly

如果您知道查询中的日期范围,则可以生成日期:

select date(date_add(day, i, "DAY")) day
from  (select '2015-01-01' day) a 
cross join
(select 
   position(
     split(
       rpad('', datediff('2015-01-15','2015-01-01')*2, 'a,'))) i 
 from (select NULL)) b;

然后,您可以将此结果与查询表结合使用:

SELECT COUNT(*) AS Key, 
       DATE(t.day) AS Value  from (...the.above.query.pasted.here...) d
left join [myDataSet.myTable] t on d.day = t.day
WHERE    ..... 
GROUP BY Value 
ORDER BY Value DESC 
LIMIT 1000;