【问题标题】:Bigquery - finding when a sum was reachedBigquery - 查找何时达到总和
【发布时间】:2018-06-21 21:15:16
【问题描述】:

我正在尝试使用 BigQuery 上的 NOAA 全球日间天气数据全球表面摘要来查找 2012 年总降水量超过 2 英寸的日期以及以下 stn 和 wban 值 722616 和 03032。

这是我目前构建的查询:

#standardSQL
select a.prcp, concat(year, mo, da) as date, a.stn, a.wban
from `bigquery-public-data.noaa_gsod.gsod*` a
where _TABLE_SUFFIX = '2012' and a.stn = '722616' and a.wban = '03032'
order by date;

但是,我不确定如何实际输出 prcp 总和 > 2 的日期值。

感谢任何帮助

谢谢!

【问题讨论】:

  • a.prcp是当年的累计降水量,还是只是某一行的降水量?
  • 它是该特定行(天)的降水量

标签: sql google-bigquery


【解决方案1】:

您可以使用累积和:

select *
from (select prcp, concat(year, mo, da) as date, stn, wban,
             sum(prcp) over (partition by year order by mo, da) as running_prcp
      from `bigquery-public-data.noaa_gsod.gsod*` g
      where _TABLE_SUFFIX = '2012' and stn = '722616' and wban = '03032'
     ) x
where running_prcp >= 2.0 and running_prcp - prcp < 2.0
order by date;

【讨论】:

  • 谢谢!你有没有机会解释查询发生了什么?更具体地说 sum(prcp) over (partition by year order by mo, da) as running_prcp
  • 仅供参考:sum(prcp) over (partition by year order by mo, da) 是一个标准的“窗口函数”,它以“累积方式”对降水求和。每行仅对前几行求和,而不是对整个集合求和。这样,你就可以知道它何时超过 2.0
【解决方案2】:

这可以在过滤prcp &gt;= 2时完成

#standardSQL
select Min(date),stn, wban, array_agg(prcp) from
(select a.prcp, concat(year, mo, da) as date, a.stn, a.wban
from `bigquery-public-data.noaa_gsod.gsod*` a
where _TABLE_SUFFIX = '2012' and a.stn = '722616' and a.wban = '03032'
order by date) f
where prcp >=
group by stn,wban

如果您对累积和超过 2.0 的日期感兴趣,请使用 Gordon 中提到的窗口函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2017-06-23
    相关资源
    最近更新 更多