Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Write Hierarchy Queries

Author  Topic 

mahesh.sanka
Starting Member

18 Posts

Posted - 2014-02-05 : 06:47:48
Hi All,

I have one table which consists of data in the following manner

Child Parent
ZN1 NS
ZN2 NS
A1 ZN1
A3 Zn1
A2 Zn2
A4 Zn2
IT1 A4
It2 A3
It3 A2
It5 A1

Now i want to display the information in the follwing manner.

Zn1 NS
A1 Zn1
It5 A1
A3 Zn1
It2 A3
Zn2 NS
A2 Zn2
It3 A2
A4 Zn2
IT1 A4


Thanks & Regards,
Mahesh Kumar Sanka

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-02-05 : 12:45:32
Whats the logic?
You could do:
select child
,parent
from yout_table
order by case child
when 'ZN1' then 0
when 'A1' then 1
when 'It5' then 2
when 'A3' then 3
when 'It2' then 4
when 'ZN2' then 5
when 'A2' then 6
when 'It3' then 7
when 'A4' then 8
when 'IT1' then 9
else 10
end
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-10 : 09:02:13
quote:
Originally posted by mahesh.sanka

Hi All,

I have one table which consists of data in the following manner

Child Parent
ZN1 NS
ZN2 NS
A1 ZN1
A3 Zn1
A2 Zn2
A4 Zn2
IT1 A4
It2 A3
It3 A2
It5 A1

Now i want to display the information in the follwing manner.

Zn1 NS
A1 Zn1
It5 A1
A3 Zn1
It2 A3
Zn2 NS
A2 Zn2
It3 A2
A4 Zn2
IT1 A4


Thanks & Regards,
Mahesh Kumar Sanka


Use a recursive cte for this
see
http://technet.microsoft.com/en-us/library/ms186243(v=sql.105).aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -