Hi guys,
I am struggling with a script to create a custom hierarchy.
Following the logic:
I have a table called hierarchy where I have son and parent column like this:
I also have a table with sons’ description:
So far, I made a hierarchy in the following SELF JOINs schema and simply adding columns representing another layer. The trick is that the requirement is to create it way differently, in one column where parents follow sons. Like this:
699 (from the above screenshot)
2163 if it has sons put them below
1
2
3 – end of hierarchy for this one
2094
4
7
8
5
6
Do you have any idea how to handle this?
Maybe some additional data will do so?
Please let me know how instead of multiple unions I can handle this.
Thanks.
Bart
JamesK
April 5, 2024, 12:31pm
2
Without some consumable data (like I have shown below), it is hard to give a reliable answer. But, something like below where I am using a recursive cte might be what you need. You might also want to investigate hierarchyid
DROP TABLE IF EXISTS #tmp;
CREATE TABLE #tmp(son INT, parent INT);
--- parent 1
-- children 2, 3
-- grandchildren 2/4, 2/5, 2/6, 3/7, 3/8
INSERT INTO #tmp (son, parent)
VALUES
(1,NULL),
(2,1), (3,1),
(4,2),(5,2),(6,2),
(7,3),(8,3);
;WITH cte AS
(
SELECT son, parent, SortingId = CAST(RIGHT(CONCAT(REPLICATE('0',10),son),10) AS VARCHAR(1024))
FROM
#tmp tp
WHERE
parent IS NULL
UNION ALL
SELECT tp.son, tp.parent,
CAST(
CONCAT(
c.SortingId,'/',
RIGHT(CONCAT(REPLICATE('0',10),tp.son),10)
)
AS VARCHAR(1024))
FROM
#tmp tp
INNER JOIN cte c ON
c.son = tp.parent
)
SELECT * from cte ORDER BY cte.SortingId;
@JamesK thank you very much for your input.
Following your logic, I need to flatter the hierarchy so that I have everything in one column. Considering your data I want to have everything in one column like this (based on numbers):
1 - parent
2 - children 1
4 - another layer of child
5
3 - children 2
7
8
This sounds really tricky I am wondering whether I can do it quicker than simply UNION each and every select statement to get it in one column.
Thanks again.
Bart
JamesK
April 9, 2024, 6:00pm
4
It is a recursive cte.You will not need to write a union for each level. Notice that the code after the UNION ALL construct references the cte - sort of like a self-reference.
Google for recursive cte in SQL Server.
I already used recursive CTE (first screenshot) and I have the relationship in two columns.
I need to flatten it to one column in the following logic:
Parent
child
grandchild
grandchild
grandchild
child
grandchild
grandchild
I have a feeling that I totally do not understand this recursive CTE concept that's why I have tons of questions.
JamesK
April 13, 2024, 1:38pm
6
If you have not already done so, copy the code I posted to a query window and run it. You will see it is doing exactly what you are describing. Then change the insert statement to add more levels or children or whatever and in every case it should generate results that show the hierarchy in the desired order.
If it does not, post the example data set and query that someone can copy and run, like I did.
Yeah, I did. I added extra rows, I understand what is happening. I make it happen with the sample data (attached). The thing is how to translate it into one column vertically, not horizontally like I posted above based on your input.
1 - parent
2 - children 1
4 - another layer of child
5
3 - children 2
7
8
Data:
47532 Null
50294 Null
2077 Null
2503 Null
730 Null
75 Null
47487 Null
728 Null
2079 Null
50307 Null
2501 Null
2078 Null
711 Null
2480 Null
50309 Null
45476 Null
40974 Null
699 Null
734 Null
2466 Null
47485 Null
2505 Null
50301 Null
2076 Null
707 Null
2047 Null
2798 Null
255664 Null
47527 Null
1018 118890
118959 118890
118952 118890
118954 118890
118941 118890
21164 34950
21166 34950
21168 34950
21170 34950
249244 34950
250298 34950
253196 34950
254393 34950
227970 125068
118947 118941
228108 250020
228105 250020
228127 250020
228124 250020
228130 250020
228111 250020
239861 125099
239859 125099
244677 174251
244685 174251
244681 174251
131837 125100
244537 125100
307689 125100
307693 125100
126539 2076
100267 2076
227771 2076
17860 2076
17868 2076
53256 2076
2482 2076
227978 2077
227990 2077
228017 2077
228019 2077
228020 2077
105567 2077
105569 2077
105571 2077
174251 2077
174253 2077
174259 2077
174265 2077
174255 2077
174257 2077
178258 2077
178254 2077
178256 2077
178261 2077
21063 2286
98901 2286
21130 2286
32911 2286
27931 2286
133742 2286
2129 2083
2121 2083
2115 2083
31178 2083
10290 2085
10284 2085
10286 2085
10288 2085
102265 46279
240019 310514
255864 310514
49679 52476
210031 22349
210032 22349
269700 53083
246986 155489
228072 121686
228069 121686
228078 121688
228075 121688
276600 8087
96113 8087
44505 8087
102269 8087
253362 121690
253365 121690
253361 121690
253364 121690
253363 121690
57967 100267
240009 100267
120358 104382
120383 104382
120388 104382
120390 104382
120402 104382
120411 104382
120404 104382
120416 104382
18777 2047
18794 2047
18788 2047
109847 2047
18785 2047
301894 2047
133997 2047
122586 2047
53366 2047
122579 2047
290166 2047
289296 125228
289300 125228
276534 125228
309863 100655
261084 100657
227940 125234
253181 125234
206654 125234
125275 125234
116953 125256
245222 125256
227721 227661
53266 227661
227714 227661
298739 227661
284474 18788
18796 18794
18814 18800
22868 18800
134800 21240
104382 75
104260 75
113317 75
1025 75
118890 75
46875 47485
51692 47485
51696 47485
190949 47487
190954 47487
281698 47487
191074 47487
281699 47487
281700 47487
281701 47487
35346 18820
35348 18820
35350 18820
280732 227731
280738 227731
280743 227731
280736 227731
280745 227731
280747 227731
250324 250273
315298 250273
315295 250273
226356 2466
17962 2466
53259 2466
2482 2466
227813 45476
53275 45476
227853 45476
228140 45476
227962 45476
274097 47527
280767 131498
227754 131498
227752 131498
227753 131498
247271 227759
247273 227759
17891 2480
227881 2480
227882 2480
227661 2480
131498 2480
227759 2480
2482 2480
23802 2482
19859 2482
19704 2482
17894 2482
51686 2482
106562 2482
105913 2482
222117 2482
116023 2482
283085 2482
231095 310539
272038 310539
240366 310539
125220 310539
310543 310541
231161 310541
273147 232723
145356 120110
207844 154940
20623 154940
52476 40261
48196 40261
48675 40261
48189 40261
100655 40261
115605 40261
115607 40261
115609 40261
301915 40263
48677 40263
48595 40263
48679 40263
100657 40263
118379 40263
48681 40263
48666 40265
48660 40265
48668 40265
123408 40265
123412 40265
106091 40267
48651 40267
48647 40267
48645 40267
245903 40267
106096 40267
106101 40267
246184 40269
48641 40269
48643 40269
279180 40269
48629 40271
48203 40271
48627 40271
245928 40271
296529 40271
246923 40271
245938 40271
48631 40271
48623 40273
117098 40273
48610 40273
48607 40273
48625 40273
295987 40273
48579 40275
48580 40275
246160 40275
117171 40275
48581 40275
117174 40275
261889 40277
261893 40277
261895 40277
261897 40277
261899 40277
261901 40277
305373 269696
121682 2079
17960 2079
121698 2079
216070 17860
205115 17860
17864 17860
252967 17860
272493 28103
272495 28103
251651 17868
251735 17868
32575 17868
32983 17868
252970 17868
252972 17868
17874 17868
39257 17868
272505 28111
272506 28111
272514 28113
272515 28113
309408 17891
53083 17891
100691 17891
100694 17891
100696 17891
100698 17891
100700 17891
95628 17891
139108 17891
249008 17891
220191 17891
227877 17891
253725 17891
104922 17891
309399 17891
183450 17894
53925 227771
247503 227771
17889 227771
17856 227771
53944 227771
17858 227771
40287 2501
255407 2501
255400 2501
255595 2501
255597 2501
276926 2501
255598 2501
47791 2501
18820 2501
22339 2503
44725 2503
44731 2503
44539 2503
96970 2503
110787 2503
156225 2505
120110 2505
121838 2505
100836 2505
131841 100836
108426 100836
249002 100836
100838 100836
157096 100836
154974 119268
274686 227813
225478 227813
253223 227813
253208 227813
269994 51692
52055 51692
52088 51692
52090 51696
52092 51696
281930 272888
231156 227836
125226 227836
231112 227836
299641 227836
125328 227836
125319 227836
125323 227836
213380 227836
251565 227836
251548 227836
231162 227836
271263 227836
216402 227836
253157 227836
253169 227836
231164 227836
304527 227836
304522 227836
198841 227836
231149 227836
125215 227836
230707 227836
274344 227853
307608 227853
273126 227853
274345 227853
281404 227853
232725 227853
280501 227881
280500 227881
280499 227881
156249 156225
185266 17894
240023 17901
260250 116212
260252 116212
118552 48629
239853 17908
213362 17908
292640 17908
239857 17908
310751 249347
249416 249347
249385 249347
249420 249347
249418 249347
249422 249347
249426 249347
261294 48643
261296 48643
300063 48643
261298 48643
237328 116231
253235 116231
116242 116239
116249 116239
42149 116239
249347 116239
252297 116242
251454 116242
251438 116242
253244 116242
251443 116242
251446 116242
251448 116242
251450 116242
251452 116242
253990 116249
254032 116249
254026 116249
254022 116249
254015 116249
254013 116249
254030 116249
253986 116249
253988 116249
297359 48666
296644 48666
48664 48666
309526 48675
309838 48675
309347 48675
309876 48677
3063 120358
16181 120358
7314 120358
1067 120358
32054 120358
32036 120358
227025 120358
249888 120358
121686 17960
250020 17960
121690 17960
121688 17960
121694 17960
121696 17960
231163 17962
240114 17962
17974 17962
253216 17962
253213 17962
310535 17962
17982 17962
17976 17962
288537 17962
17986 17962
304163 17962
240104 17962
250054 48683
105520 47532
288794 47532
110541 47532
107939 47532
107947 47532
138803 47532
107955 47532
270964 47532
245484 47532
133736 133734
133738 133734
32838 133734
32907 133734
232723 227962
252618 227962
244541 227962
273153 227962
253236 227962
227981 227978
227979 227978
23623 227978
23639 227978
250517 250511
227993 227990
23633 227990
297428 227990
227991 227990
227997 227990
227995 227990
228004 227990
297797 113317
1015 113317
118179 113317
1014 113317
250141 113317
118067 113317
217675 113317
19047 113317
229610 113317
45804 113317
235548 228017
235549 228017
300654 242355
300656 242355
300650 242355
300658 242355
23618 228020
23490 228020
23616 228020
215507 228020
23485 228020
253198 228020
228022 228020
307671 228020
306862 228020
23492 228020
203198 21178
203193 21178
203195 21178
2163 699
2094 699
8568 699
2087 699
107729 699
2085 699
2084 699
2093 699
2088 699
2091 699
2252 699
975 699
2082 699
2083 699
2110 699
2089 699
2086 699
2268 699
2248 699
2103 699
2090 699
272888 707
272896 707
307620 135878
254581 135878
254583 135878
254586 135878
262833 135878
254567 135878
30793 711
30792 711
30791 711
30789 711
30784 711
30783 711
30782 711
30787 711
30786 711
30785 711
8972 711
8978 711
8982 711
8974 711
30777 711
30775 711
8976 711
2279 119507
53589 119507
119657 728
119678 728
119690 728
8087 728
8089 728
253146 728
216234 728
250511 730
249725 730
123346 730
21234 730
21232 730
15190 730
39208 730
21213 730
21209 730
250164 730
934 730
925 730
34950 730
21885 730
21240 730
21881 730
21248 730
21244 730
21871 730
21242 730
310121 48698
120462 120383
1080 120383
1084 120388
1065 120388
253372 120390
253373 120390
253374 120390
253375 120390
253376 120390
50460 126539
261024 126539
223714 126539
1086 120404
16166 120404
207953 120402
207955 120402
207951 120402
1083 120402
1069 120402
224354 120402
1099 120411
120486 120411
120482 120411
223063 120416
7319 120416
1090 120416
1092 120416
309357 106091
307118 106091
18804 255598
227913 124581
227919 124581
249242 124581
125276 124581
227922 124581
227916 124581
227932 124588
227935 124588
227929 124588
227926 124588
18800 255664
110909 44731
243440 212667
243431 212667
243419 212667
272520 268018
272521 268018
156449 2799
100402 46875
51989 46875
52137 46875
119865 46875
154711 46875
306246 46875
283571 16181
31629 22339
22349 22339
210010 22339
210012 22339
119524 119516
285551 734
285541 734
285520 734
255851 734
135878 734
133734 734
231108 240359
310541 240359
310539 240359
253183 240359
306039 240359
227844 240359
122150 2798
50835 2798
260380 2798
122069 2798
120719 2798
102016 2798
120142 2798
164706 2798
269388 2798
144306 2798
154476 2798
51225 2798
246774 2798
247012 2798
265278 2798
244011 2798
269839 2798
258397 2798
116468 2798
212667 2798
243990 2798
51258 2798
234960 2798
223442 2798
235349 2798
52316 2798
256126 2798
96533 2798
119268 2798
105955 2798
155446 2798
154940 2798
255025 2798
155489 2798
155460 2798
223440 2798
155492 2798
301258 2798
285416 2798
290708 2798
285136 2798
285437 2798
235371 2798
267010 2798
248811 2798
51228 2798
174867 2798
254304 2798
109883 2798
51230 2798
29086 2798
47666 2798
47700 2798
2799 2798
20526 2798
20559 2798
2859 2798
2827 2798
2844 2798
20568 2798
20517 2798
20532 2798
9785 2798
2958 2798
2974 2798
20562 2798
20580 2798
2895 2798
20605 2798
2938 2798
47387 21234
21217 21234
47389 21234
21219 21234
47406 21232
21178 21232
242129 21232
21172 21232
9280 8972
9282 8972
9284 8972
9286 8972
9288 8972
9290 8972
9292 8972
21358 8972
21360 8972
21362 8972
21364 8972
21366 8972
21368 8972
21408 8976
9298 8976
9306 8976
9304 8976
9300 8976
9296 8976
9302 8976
9308 8978
9310 8978
9312 8978
9468 8982
9483 8982
9476 8982
9479 8982
9460 8982
9457 8982
9485 8982
9481 8982
9472 8982
9470 8982
9462 8982
9466 8982
9474 8982
9455 8982
9464 8982
307754 228140
253232 228140
307748 228140
307700 228140
40263 115535
40261 115535
40265 115535
40267 115535
40269 115535
240556 115535
40271 115535
40273 115535
40275 115535
40277 115535
116099 115537
116212 115537
116227 115537
116214 115537
242123 115537
48683 115537
116231 115537
48692 115537
116229 115537
48688 115537
48698 115537
249391 115537
228057 121682
228042 121682
228036 121682
228033 121682
228051 121682
228054 121682
228039 121682
280511 121682
285522 285520
15199 15190
15194 15190
15196 15190
15192 15190
228133 121696
228136 121696
232973 121698
228099 121698
228096 121698
280522 121698
210014 22339
210016 22339
210018 22339
1016 104260
119507 104260
119516 104260
1019 104260
221205 155460
220911 155460
48537 155460
285559 285541
2275 285541
2286 285541
228091 121694
152194 121694
228084 121694
277258 119657
45428 119657
45424 119657
45432 119657
274629 119657
16929 119657
45430 119657
45426 119657
110800 119657
46016 119657
285556 285551
285553 285551
2271 285551
275453 119678
275584 119678
275585 119678
275586 119678
275587 119678
275588 119678
216227 119678
29016 119678
304922 52092
278617 119690
278601 119690
278575 119690
96111 119690
46279 119690
96109 119690
216238 119690
278541 119690
216240 119690
209467 31629
210026 31629
210027 31629
242359 240556
242093 240556
242355 240556
261316 240556
180504 121838
119482 1016
309868 1016
113299 1015
113304 1015
113309 1015
19026 1015
246659 1015
253021 1015
232711 1019
271184 1025
271204 1025
271198 1025
268021 1025
28103 1025
272494 1025
28111 1025
1028 1025
28113 1025
268018 1025
268975 1025
306409 1025
306412 1025
271172 1025
271174 1025
271179 1025
272509 1028
272510 1028
124117 29704
227827 29704
198925 29704
230821 29704
305029 220191
117103 295987
117153 295987
307991 226356
17885 226356
269696 226356
272012 226356
249967 226356
254430 226356
227732 53256
227733 53256
227731 53256
227747 53259
227740 53259
241233 53259
310744 40974
115535 40974
115537 40974
116239 40974
280802 53266
280801 53266
280799 53266
280800 53266
280803 53266
280804 53266
232743 53275
232769 53275
232759 53275
286228 53275
124588 2078
125234 2078
125256 2078
124581 2078
310514 2078
125068 2078
125085 2078
250293 2078
250295 2078
250291 2078
125099 2078
250342 2078
125100 2078
124260 2078
17901 2078
17908 2078
99309 2078
250273 2078
33702 2084
33731 2084
33724 2084
33735 2084
10282 2093
10280 2093
10278 2093
10276 2093
30396 2093
2238 2090
2223 2090
2209 2090
2243 2090
2231 2090
254432 226356
210035 210010
252978 50294
127093 50294
127084 50294
127091 50294
127080 50294
2482 50294
227836 50301
240359 50301
125228 50301
29704 50301
2482 50301
227661 50309
53256 50309
53259 50309
131498 50309
227759 50309
253393 50307
124123 50307
124139 50307
124151 50307
284471 50307
297606 50307
253394 50307
124153 50307
17955 50307
124125 50307
124141 50307
17970 50307
124127 50307
124137 50307
124135 50307
2482 50307
49780 2082
285564 2275
285563 2275
285565 2275
32830 2275
32913 2275
224914 46279
278624 46279
279263 46279
224867 46279
279255 46279
57950 46279
96656 46279
96106 46279
Ifor
April 15, 2024, 12:29pm
8
Pepper1991:
Yeah, I did. I added extra rows, I understand what is happening. I make it happen with the sample data (attached). The thing is how to translate it into one column vertically, not horizontally like I posted above based on your input.
This is what @JamesK did. The other columns just show you where the order is coming from. If you just want one column just SELECT Son; you will still need to sort by SortingId.