I'm looking to tackle a difficult SQL query. I'd appreciate some input on how to achieve this relatively complex SQL query on a table.
I currently have a table as follows:
caseId scanId attribute
-----------------------
1 2 A
1 4 A
2 3 B
3 NULL NULL
I want to be able to achieve this table:
caseId scanId1 scanId2 attribute1 attribute2 count
--------------------------------------------------
1 2 4 A A 2
2 3 NULL B NULL 1
3 NUL NULL NULL NULL 0
I know this involves joining the table on itself, however I can't come up with how to dynamically make the number of columns (scanId1, scanId2, etc.) required, which depends on the number of unique caseId tuples.
Any tips on how I can get this to work?
Thanks in advance!