基于其他列条件的累计和
Posted
技术标签:
【中文标题】基于其他列条件的累计和【英文标题】:Cumulated sum based on condition in other column 【发布时间】:2019-05-08 15:14:38 【问题描述】:我想根据以下结构的数据创建一个视图:
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);
我想只累积连续几天的降水。
以下是不同的测试用例的预期结果 - 除了应该省略没有下雨的日子:
我已尝试使用 OVER(PARTITION BY date, rain_on_day)
的窗口函数,但它们没有产生预期的结果。
我该如何解决这个问题?
【问题讨论】:
结果应该真正匹配给定的测试用例。 【参考方案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
返回所有下雨天以及连续几天的累计总和(以及一个跑步组编号)。
基础知识:
Select longest continuous sequence【讨论】:
您确实看到小提琴证明它有效吗?是否缺少任何关键信息?喜欢你的 Postgres 版本吗?【参考方案2】:这是一种无需明确列举所有日期即可计算累积降水量的方法:
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 |
【讨论】:
以上是关于基于其他列条件的累计和的主要内容,如果未能解决你的问题,请参考以下文章