Your image from the comment (which should be text in the question) shows that you run the query with this WHERE
clause:
WHERE ... AND ("SupplierFacilityId" IS NULL OR "SupplierFacilityId" = 647)
So you can just drop "SupplierFacilityId"
from PARTITION BY
to achieve what you want: rows with "SupplierFacilityId" IS NULL
are treated as equal peers of the partition:
sum(s."QuantityChange") OVER (PARTITION BY s."LocationId", s."PartId"
ORDER BY s."DueDate" ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS "RunningSum"
Also, since the default framing option is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
, and assuming that (s."LocationId", s."PartId", s."DueDate")
is defined unique (which makes RANGE
and ROWS
equivalent in this context), you can just drop the custom framing options:
sum(s."QuantityChange") OVER (PARTITION BY s."LocationId", s."PartId"
ORDER BY s."DueDate") AS "RunningSum"
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 ...
s.LocationId
is1
,s.PartId
is2
ands.SupplierFacilityId
isNULL
. So where is the problem?SupplierFacilityId
arenull, 1, 1, 2, 3
which of those values do you want to add its sum to? Or do you want a separate group for the null? From the screenshot you link above, it's unclear whySupplierFacilityId
is being grouped at all