导致重复的 Postgres 行

Postgres Rows Between Causing Duplicates

我有一个简单的查询,用于计算当前行和前面 11 行的值之和。当行数大于 12 时它工作正常,但当它小于 12 时,数据会被复制以填充缺失值。

总计 TABLE:

ID|Report_Month| Total
1 |2018-08-01 |5
2 |2018-09-01 |25
3 |2018-10-01  |15

示例代码:

select distinct 
         ID,
         Report_Month,
         Total,
         sum(Total) over (partition by ID order by report_month rows between 11 preceding and current row) as Running_Total
from TOTALS_TABLE;

预期输出:

ID|Report_Month|Total|Running_Total
1 | 2018-08-01 | 5 | 5
2 | 2018-09-01 | 25 | 30
3 | 2018-10-01 | 15 | 45

实际输出:

1 | 2018-08-01 | 5 | 5
1 | 2018-08-01 | 5 | 10
1 | 2018-08-01 | 5 | 15
1 | 2018-08-01 | 5 | 20
2 | 2018-09-01 | 25 | 45
2 | 2018-09-01 | 25 | 70
2 | 2018-09-01 | 25 | 95
2 | 2018-09-01 | 25 | 120
3 | 2018-10-01 | 15 | 135
3 | 2018-10-01 | 15 | 150
3 | 2018-10-01 | 15 | 165
3 | 2018-10-01 | 15 | 180

任何帮助将不胜感激,我觉得我很接近可能遗漏了什么。

您似乎想要一个对不同 ID 求和的查询,但您已经告诉总和按 ID 进行分区,这意味着您的 运行 总数将在每次 ID 更改时重置(== 没有办法您发布的查询可以产生您发布的结果,即使 Postgres 自发地发明行以提供一些总结)。删除分区

https://www.db-fiddle.com/#&togetherjs=fw7TIVul3H

我没有遇到重复行问题,我不明白为什么添加分析会导致它。我认为您的来源 table 或查询确实有重复的行(我认为您对 distinct 的使用试图删除它们)并且分析工作正常。做一个

Select * from totals_table 

并检查你的数据是否正常。如果它有重复的行,你不能用你所拥有的方式用 distinct 删除它们,因为 distinct 考虑了 运行 总数的结果(并且它使每一行都是唯一的)。最好在源头上解决重复问题,而不是稍后尝试将它们区分开来,但是如果您打算这样做,则必须在内部查询中进行区分,而 运行 total 在外部查询

线索是select distinct。这应该没有必要。如果基础 table 有重复项,您应该修复它。同时,您可以尝试调整查询。

我不确定正确的解决方法是什么。这里有两种可能。

如果整行重复:

select ID, Report_Month, Total,
       sum(Total) over (partition by ID order by report_month rows between 11 preceding and current row) as Running_Total
from (select distinct tt.*
      from TOTALS_TABLE tt
     ) tt;

如果总计 table 有每个 dy 的小计需要相加:

select ID, Report_Month,
       sum(Total) as month_total,
       sum(sum(Total)) over (partition by ID order by report_month rows between 11 preceding and current row) as Running_Total
from TOTALS_TABLE tt
group by id, Report_Month;