我如何使用 standardSQL (BigQuery) 使几个月有所不同

How do I make that difference from months using standardSQL (BigQuery)

我有以下查询:

#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 时,出现以下错误:

未找到函数:年份 [5:180]

我知道这是因为我使用的是标准 SQL,但我如何才能与使用标准 SQL 的几个月有所不同?

BigQuery 中的 StandardSQL 支持用于提取日期部分的 ISO/ANSI-standard 函数。这是 extract():

你想要:

extract(year from <datecol>)
extract(month from <datecol>)

这在 documentation 中有解释。

要查找两个日期之间的月份差异,您最好使用 DATE_DIFF()

DATE_DIFF(DATE_END, DATE_START, MONTH)