I have a query that returns the following dataset:
RELATIONid MAPid D1id D2id D3id
4999 4999 626 1250 7
5000 5000 626 1250 8
For the next step, I need to bind those datasets into treeview (hierarchy structure). I need to transform this dataset into the following:
Nodeid ParentNodeid Header
626 null D1
1250 626 D2
7 1250 D3
8 1250 D3
How I can achieve those structure from the original dataset?
I have one more favor to ask, the data structure a (little bit) more complex than the previous one. Let's say I have sample dataset like this:
RELATIONid MAPid D1id D2id D3id
4999 4999 626 1250 7
5000 5000 626 1250 8
5001 5001 627 1300 10
5002 5002 627 1300 12
5003 5003 628 1400 15
From the following dataset, we have 3 MainParent: 626, 627, 628
and the transformation (cross apply) output expectation will be like this:
Nodeid ParentNodeid Header
626 null D1
1250 626 D2
7 1250 D3
8 1250 D3
627 null D1
1300 627 D2
10 1300 D3
12 1300 D3
628 null D1
1400 628 D2
15 1400 D3
Please note that the data are ordered per ParentNode
followed by its node data.
Best Answer
A CROSS APPLY would seem perfect for the job:
For every row of the source dataset, CROSS APPLY produces three, using the VALUES row constructor, explicitly specifying which column of the original set goes into which new column.
Now, the above will return duplicates if some pairs in your source repeat. You can suppress them with DISTINCT:
In order for the transformed set to follow the order of
D1id ASC, D2id ASC, D3id ASC
, you could include those columns in the output and use them for sorting:The reason you have to include them in SELECT is because, when you have DISTINCT, you may only sort by columns in the SELECT clause. Naturally, the result set will include the three extra columns as well. If you do not want them in the output, you can use the above as a derived table: your outer SELECT would pull only the three required columns and sort by the other three:
Alternatively, you could use GROUP BY instead of DISTINCT and thus return the sorted three-column set without a derived table: