I have some data that comes out of a cube.
I have one row in this order that has gone in as a substitution variance but I need to put it in the quantity variance bucket. The code that decides on whether it's a substitution is here at the lowest level (this is within a large CTE) (The code behind this cube is massive and exists on multiple levels)
CASE
WHEN ISNULL(PC.ItemDimCostPrice, SC.ItemDimCostPrice) = 1
AND (PC.ConfigId <> SC.ConfigId
OR PC.InventColorId <> SC.InventColorId
OR PC.InventSizeId <> SC.InventSizeId
OR PC.InventStyleId <> SC.InventStyleId)
THEN 1
--New code here ... THEN 0
WHEN PC.[Resource] IS NULL
OR SC.[Resource] IS NULL
THEN 1
ELSE 0
END AS [HasSubstitutionVariance] ,
The logic that they have given me which I need to add into this is basically if the cost grouping is material (I know how to calculate this) and there exists another record with the same costgroupid and operationid (within the production order) then this will never be a substitution variance. I was thinking of using rownumber and partitioning on costgroupid and operationid but as far as I'm aware you can't use window functions in a case statement? Anyone have any ideas how I would do this? Thanks!
CASE
is SQL Server is an expression - not a statement. It's an expression that can return a single value based on some conditions - it's not a statement that can control program flow etc.