March 11, 2016 at 7:21 am
Hi All,
I want to sort the name based on 1st level (sort only applicable root id is null or level ==1). If i search based on name the result set is broken the parent and child relationship.
I am using sql server database. Please let me know what are the fesible way to sort based on 1st level. I am having 5Lakh records. Also, I am considering the performance.
ID PARENT ID ROOT ID NAME level
===============================================
1NULLNULLFIRST1
211SECOND2
321THIRD3
411FORTH4
541FIFTH5
6 NULLNULLSIXTH1
766SEVENTH2
876EIGTH2
9NULLNULLNINTH1
10 NULLNULLTENTH1
11NULLNULLELEVEN1
12 1111TWELE2
131211THIRTEEN3
141311FOURTEEN4
EXPECTED OUTPUT - SORT BY NAME ASC
====================================
ID PARENT ID ROOT ID NAME level
===============================================
11NULLNULLELEVEN1
12 1111TWELE2
131211THIRTEEN3
141311FOURTEEN4
1NULLNULLFIRST1
211SECOND2
321THIRD3
411FORTH4
541FIFTH5
9NULLNULLNINTH1
6 NULLNULLSIXTH1
766SEVENTH2
876EIGTH2
10 NULLNULLTENTH1
March 11, 2016 at 12:44 pm
Look at using a recursive CTE to walk through the hierarchy. If you need additional help please post the DDL (CREATE TABLE statement) for the table(s) involved, sample data (as INSERT INTO statements) and the expected results based on the sample data.
March 12, 2016 at 10:34 am
stsivaraj (3/11/2016)
Hi All,I want to sort the name based on 1st level (sort only applicable root id is null or level ==1). If i search based on name the result set is broken the parent and child relationship.
I am using sql server database. Please let me know what are the fesible way to sort based on 1st level. I am having 5Lakh records. Also, I am considering the performance.
ID PARENT ID ROOT ID NAME level
===============================================
1NULLNULLFIRST1
211SECOND2
321THIRD3
411FORTH4
541FIFTH5
6 NULLNULLSIXTH1
766SEVENTH2
876EIGTH2
9NULLNULLNINTH1
10 NULLNULLTENTH1
11NULLNULLELEVEN1
12 1111TWELE2
131211THIRTEEN3
141311FOURTEEN4
EXPECTED OUTPUT - SORT BY NAME ASC
====================================
ID PARENT ID ROOT ID NAME level
===============================================
11NULLNULLELEVEN1
12 1111TWELE2
131211THIRTEEN3
141311FOURTEEN4
1NULLNULLFIRST1
211SECOND2
321THIRD3
411FORTH4
541FIFTH5
9NULLNULLNINTH1
6 NULLNULLSIXTH1
766SEVENTH2
876EIGTH2
10 NULLNULLTENTH1
This is actually very easy to do but I don't have the time to make your data readily consumable to show you actual working code. Please see the first link under "Helpful Links" in my signature line below to help us help you. Thanks and welcome aboard.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 13, 2016 at 11:23 pm
Thanks for your response. Actually, I got solution from my manager. Thanks for your support
March 14, 2016 at 9:29 am
stsivaraj (3/13/2016)
Thanks for your response. Actually, I got solution from my manager. Thanks for your support
Forum etiquette would have you post your solution for others to see as your solution may be helpful to others.
March 14, 2016 at 9:47 am
stsivaraj (3/13/2016)
Thanks for your response. Actually, I got solution from my manager. Thanks for your support
I'd be very interested in seeing the code that you and your manager used. Most managers get this stuff wrong performance and utility wise.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply