【问题标题】:How do I make that difference from months using standardSQL (BigQuery)如何使用标准 SQL (BigQuery) 与几个月不同
【发布时间】:2018-07-20 14:31:13
【问题描述】:

我有以下疑问:

#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]

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

【问题讨论】:

    标签: sql google-bigquery bigquery-standard-sql


    【解决方案1】:

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

    DATE_DIFF(DATE_END, DATE_START, MONTH)
    

    【讨论】:

      【解决方案2】:

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

      你想要:

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

      这在documentation中有解释。

      【讨论】:

        猜你喜欢
        • 2016-11-23
        • 2018-09-10
        • 1970-01-01
        • 2017-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多