sql - postgres 中 over 子句内的 Where 子句

标签 sql postgresql partitioning greenplum

是否可以像下面这样在覆盖子句中使用 where 子句?

SELECT SUM(amount) OVER(partition by prod_name WHERE dateval > dateval_13week)

我不能在 over 子句中使用前面和后面,因为我的日期没有按顺序排列。 我需要获取的是小于当前记录的 13 周日期值的记录。

EDIT : 
sum(CASE WHEN dateval >= dateval_13week and dateval <=current_row_dateval then amount else 0 end) over (partition by prod_name order by week_end desc)

只是为了详细说明,早些时候当我将所有日期按顺序排列时,我使用以下查询对记录进行分区。现在我的日期是随机排列的,并且缺少一些日期。

sum(amount) over 
        (partition by prod_name order by prod_name,week_end desc rows between 0 preceding and 12 following)

最佳答案

添加到@D Stanley 的回答中,您可以在 Postgre 中使用 FILTER 子句作为聚合函数:

SELECT SUM(amount) FILTER (WHERE dateval > dateval_13week)
           OVER(partition by prod_name)

关于sql - postgres 中 over 子句内的 Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22149677/

相关文章:

sql - Greenplum plpgsql 函数在输入结束时返回语法错误

postgresql - 难道超过几十个分区没有意义吗?

sql - 将包含数组的行拆分为包含子数组的多行

mysql - ~ 符号在 MySQL 中意味着什么?

sql - 具有不同参数的 2 个相同的 sql 查询 - 只有一个需要临时表

c# - 如何找到集合的所有分区

以所有独特方式在存储桶之间分区/分配总和的算法

php - MySQL 错误 : 'Unkown column' tablename. 列名在 'on clause'

sql - Postgresql COALESCE 没有设置默认值

postgresql - 如何在postgresql中生成序列