【问题标题】:Distinct with Aggregation and Analytical functions与聚合和分析功能不同
【发布时间】:2020-03-17 20:21:46
【问题描述】:

我只是好奇,有没有办法在 Postgres 中做到这一点?

SUM(DISTINCT column_a) OVER(PARTITION BY column_b, column_c)

使用 DISTINCT 会导致错误:DISTINCT 未针对窗口函数实现

【问题讨论】:

标签: postgresql distinct window-functions


【解决方案1】:

这应该可以解决问题:

SELECT column_a,
       column_b,
       column_c,
       sum(column_a) FILTER (WHERE is_new) OVER w
FROM (SELECT column_a,
             column_b,
             column_c,
             column_a IS DISTINCT FROM lag(column_a) OVER w AS is_new
      FROM atable
      WINDOW w AS (PARTITION BY column_b, column_c ORDER BY column_a)
     ) AS q
WINDOW w AS (PARTITION BY column_b, column_c ORDER BY column_a);

在内部查询中,column_a 的所有重复项都会得到is_new = FALSE,因此这些重复项不计入外部查询。

【讨论】:

  • 哇!感谢您的帮助。似乎按预期工作。
猜你喜欢
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-02
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
相关资源
最近更新 更多