Postgres SUM over PARTITION BY "tolerate" null values I have a view which calculates rolling sum taking in value form previous row based on PARTITION BY clause, and I need to indicate one field in PARTITION BY clause can have nulls in them.
sum(s.&quot;QuantityChange&quot;) OVER (PARTITION BY s.&quot;LocationId&quot;, s.&quot;PartId&quot;, s.&quot;SupplierFacilityId&quot; ORDER BY s.&quot;DueDate&quot; ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS &quot;RunningSum&quot;

Sometimes SupplierFacilityId is null and that's fine, it should still be included in the sum() calculation.
Is this possible with Postgres?
Your image from the comment (which should be text in the question) shows that you run the query with this WHERE clause:
WHERE ... AND (&quot;SupplierFacilityId&quot; IS NULL OR &quot;SupplierFacilityId&quot; = 647)

So you can just drop &quot;SupplierFacilityId&quot; from PARTITION BY to achieve what you want: rows with &quot;SupplierFacilityId&quot; IS NULL are treated as equal peers of the partition:
sum(s.&quot;QuantityChange&quot;) OVER (PARTITION BY s.&quot;LocationId&quot;, s.&quot;PartId&quot;
                              ORDER BY s.&quot;DueDate&quot; ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS &quot;RunningSum&quot;

Also, since the default framing option is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, and assuming that (s.&quot;LocationId&quot;, s.&quot;PartId&quot;, s.&quot;DueDate&quot;) is defined unique (which makes RANGE and ROWS equivalent in this context), you can just drop the custom framing options:
sum(s.&quot;QuantityChange&quot;) OVER (PARTITION BY s.&quot;LocationId&quot;, s.&quot;PartId&quot;
                              ORDER BY s.&quot;DueDate&quot;) AS &quot;RunningSum&quot;

Without said WHERE clause, your goal would be unachievable with a simple window function as you would have to use the same rows with NULL values in multiple partitions. You would have to multiply those rows first ...