I have a table as below which has a relationship between the requestID and PrimaryRequestID:
RequestID PrimaryRequestID Prefix
-----------------------------------------------------------
1 NULL MyPrefix
2 1 NULL
What I want to do is join this so I get the following output:
1 NULL MyPrefix
2 1 MyPrefix
I’ve tried several joins, but I still end up with a NULL prefix.
Edit: This is probably as close as I’ve gotten:
SELECT
ra.RequestID
,CASE WHEN rb.Prefix IS NULL THEN ra.Prefix ELSE ra.Prefix END AS Prefix
FROM requests ra
LEFT JOIN requests rb on ra.RequestID = rb.PrimaryRequestID
Try: