我有以下查询:
#standardSQL
SELECT distinct (grand_total/months) AS avg, ((grand_total/days)) AS
avg_day
FROM
(select count(searchint.id) as Total, (DATE_DIFF(DATE ({{DATE_END}}),
DATE ({{DATE_START}}), DAY)+1) AS days, ((12 * YEAR(TIMESTAMP({{DATE_END}})) +
MONTH(TIMESTAMP({{DATE_END}}))) - (12 * YEAR(TIMESTAMP({{DATE_START}}))
+ MONTH(TIMESTAMP({{DATE_START}}))) +1) AS months,
(select count(searchint.id) as Total
from `dbsearch`
where cast(replace(searchint.createdDate,'Z','')as DateTime) >=
cast({{DATE_START}} as DateTime)
and cast(replace(searchint.createdDate,'Z','')as DateTime) <=
cast(DATE_ADD(cast({{DATE_END}} as date), Interval 1 day ) as DateTime)) AS grand_total
from `dbsearch`
where cast(replace(searchint.createdDate,'Z','')as DateTime) >=
cast({{DATE_START}} as DateTime)
and cast(replace(searchint.createdDate,'Z','')as DateTime) <=
cast(DATE_ADD(cast({{DATE_END}} as date), Interval 1 day ) as DateTime)
group by date(cast(replace(searchint.createdDate,'Z','')as DateTime))
ORDER BY 2 DESC) AS groupby
但是,当我尝试运行BigQuery时,出现以下错误:
未找到功能:YEAR年[5:180]
我了解这是因为我使用的是standardSQL,但是如何与使用standardSQL的月份区别开来?
答案 0 :(得分:2)
BigQuery中的StandardSQL支持提取日期部分的ISO / ANSI标准函数。这是extract()
:
您要
extract(year from <datecol>)
extract(month from <datecol>)
这在documentation中有解释。
答案 1 :(得分:2)
要找出两个日期之间月份的差异,最好使用DATE_DIFF()
DATE_DIFF(DATE_END, DATE_START, MONTH)