Where clause inside an over clause in postgres Where clause inside an over clause in postgres sql sql

Where clause inside an over clause in postgres


Adding to @D Stanley answer you can use FILTER clause for aggregate function in Postgre:

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


You could simulate the WHERE in your SUM parameter:

SELECT SUM(CASE WHEN dateval > dateval_13week THEN amount ELSE 0 END)            OVER(partition by prod_name)


You cannot filter the rows with the WHERE clause, inside the OVER partition clause.You can fix the query selecting only the rows that are needed to you, using CASE and performing a sum of the amount where the condition is satisfied.