I have been looking for the solution to this in PIVOT, UNPIVOT, and others but still don't see my scenario. I have items in a table. For simplicity we'll just say PartNum, Desc. These things can be customized. The attributes like color, height, width, depth are stored in a separate table with a code to indicate which attribute.
OrderId - PartNum - Desc (join from inv)
1 12345 - Block A
2 12345 - Block A
3 23456 - Block B
4 23456 - Block B
Two customers get 12345, and two get 23456 and they have width, height, and depth...
AttrId - OrderId - CCode - Value
1 1 WIDTH 10
2 1 HEIGHT 10
3 1 DEPTH 1
4 2 WIDTH 20
5 2 HEIGHT 10
6 2 DEPTH 1
7 3 WIDTH 10
8 3 HEIGHT 20
9 3 DEPTH 2
10 4 WIDTH 10
11 4 HEIGHT 20
12 4 DEPTH 2
I can't use pivot with an aggregate on the value because I need to group each combination of part, width, height, and depth like this
PartNum - Width - Height - Depth - Count - Area (w x h x count)
12345 10 10 1 1 100
12345 20 10 1 1 200
23456 10 20 2 2 400
I tried case statements with the CCode but I get null values in some rows so the grouping didn't work. This is in SQL Server 2019 if that makes a difference. Can someone help out with this?