根据其他栏中的条件求和

时间:2019-05-08 15:14:38

标签: sql postgresql window-functions gaps-and-islands cumsum

我想基于以下结构的数据创建视图:

CREATE TABLE my_table (
  date date,
  daily_cumulative_precip float4
);

INSERT INTO my_table (date, daily_cumulative_precip)
VALUES
  ('2016-07-28', 3.048)
, ('2016-08-04', 2.286)
, ('2016-08-11', 5.334)
, ('2016-08-12', 0.254)
, ('2016-08-13', 2.794)
, ('2016-08-14', 2.286)
, ('2016-08-15', 3.302)
, ('2016-08-17', 3.81)
, ('2016-08-19', 15.746)
, ('2016-08-20', 46.739998);

我只想连续几天累积降水。

以下是 不同测试用例 的理想结果-除了应避免无雨的日子:

enter image description here

我已经尝试使用OVER(PARTITION BY date, rain_on_day)的窗口函数,但是它们不能产生预期的结果。

我该如何解决?

2 个答案:

答案 0 :(得分:1)

这是一种无需明确枚举所有日期即可计算累积降水的方法:

SELECT date, daily_cumulative_precip, sum(daily_cumulative_precip) over (partition by group_num order by date) as cum_precip
FROM
    (SELECT date, daily_cumulative_precip, sum(start_group) over (order by date) as group_num
    FROM
        (SELECT date, daily_cumulative_precip, CASE WHEN (date != prev_date + 1) THEN 1 ELSE 0 END as start_group
        FROM
            (SELECT date, daily_cumulative_precip, lag(date, 1, '-infinity'::date) over (order by date) as prev_date
            FROM my_table) t1) t2) t3

收益

|       date | daily_cumulative_precip | cum_precip |
|------------+-------------------------+------------|
| 2016-07-28 |                   3.048 |      3.048 |
| 2016-08-04 |                   2.286 |      2.286 |
| 2016-08-11 |                   5.334 |      5.334 |
| 2016-08-12 |                   0.254 |      5.588 |
| 2016-08-13 |                   2.794 |      8.382 |
| 2016-08-14 |                   2.286 |     10.668 |
| 2016-08-15 |                   3.302 |      13.97 |
| 2016-08-17 |                    3.81 |       3.81 |
| 2016-08-19 |                  15.746 |     15.746 |
| 2016-08-20 |                   46.74 |     62.486 |

答案 1 :(得分:1)

SELECT date
     , dense_rank() OVER (ORDER BY grp) AS consecutive_group_nr  -- optional
     , daily_cumulative_precip
     , sum(daily_cumulative_precip) OVER (PARTITION BY grp ORDER BY date) AS cum_precipitation_mm
FROM  (
   SELECT date, t.daily_cumulative_precip
        , row_number() OVER (ORDER BY date) - t.rn AS grp
   FROM  (
      SELECT generate_series (min(date), max(date), interval '1 day')::date AS date
      FROM   my_table
      ) d
   LEFT   JOIN (SELECT *, row_number() OVER (ORDER BY date) AS rn FROM my_table) t USING (date)
   ) x
WHERE  daily_cumulative_precip > 0
ORDER  BY date;

db <>提琴here

返回所有雨天,其中包含连续几天的累积金额(以及运行组编号)。

基础:

相关问题