Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have data in table as shown in below format
like india is parent of anddhrapradesh is parent of
Nellore and Nellore is Parent of pincode

   ID     ParentID       TYpe               TExt
 1	  0	           Country       	India
2 	  1	           state	       AndhraPradesh
3	  2	           City	               Nellore
4	  3	           Pincode  	       524001
5     0	         Country	        Pakisthan
6     5           Sate                PakState
7     6           City                 Hyderabad
8     7          PinCode               251140



i want display the above data in grid view as shown in below
country          State                 City 
--------          -------            --------
 india            AndhraPradesh         Hyderabad


please guys i tried all the ways but i didn't got that one please help me out if not i will be fired 
:(
thanks in advance
Posted
Updated 26-Feb-16 5:39am
v3
Comments
OriginalGriff 1-Jan-16 10:17am    
"i tried all the ways"
What have you tried?
Where are you stuck?
What help do you need?

And don't try to guilt us into doing your work for you: if you are in a job that you can't do, then you are depriving the person who can do it of a post. That doesn't make me want to help you...
jame01 1-Jan-16 12:04pm    
your question is bit unclear but you can create view in
sql server but just adding view and select which column you want
that called join ,
and if you want do that in c# you have to create query and load to view

1 solution

Probably a bit late to stop you being fired but this solution works

First create a recursive CTE that will traverse the hierarchy and determine the "ultimate" parent of each item.
SQL
;WITH CTE AS (
	SELECT ID, ParentID, [TYpe], [TExt]
,MtoE = CAST(ID AS VARCHAR(MAX))
FROM demo
WHERE ParentID = 0
UNION ALL
SELECT e.ID, e.ParentID, e.[TYpe], e.[TExt]
, MtoE
FROM demo e
INNER JOIN CTE ecte ON ecte.ID = e.ParentID
)
SELECT EC.ID AS ID, EC.ParentID, EC.[TYpe], EC.[TExt], MtoE
FROM CTE EC
LEFT JOIN demo E ON EC.ParentID = E.ID
order by MtoE

This produces the results
Id      Parent  TYpe    TExt             MtoE
1	0	Country	India	         1
2	1	state	AndhraPradesh	 1
3	2	City	Nellore	         1
4	3	Pincode	524001	         1
5	0	Country	Pakisthan	 5
6	5	State	PakState	 5
7	6	City	Hyderabad	 5
8	7	PinCode	251140	         5

The derived column MtoE is the ID of the "top" level for each set of data. That dataset can be fed into a pivot instead of the simple query above
SQL
;WITH CTE AS (
	SELECT ID, ParentID, [TYpe], [TExt]
,MtoE = CAST(ID AS VARCHAR(MAX))
FROM demo
WHERE ParentID = 0
UNION ALL
SELECT e.ID, e.ParentID, e.[TYpe], e.[TExt]
, MtoE
FROM demo e
INNER JOIN CTE ecte ON ecte.ID = e.ParentID
)
SELECT Country, [state], City, Pincode
FROM (
    SELECT MtoE,[Type], [TExt]
    FROM CTE
) as s
PIVOT
(
    MAX([TExt])
    FOR [TYpe] IN (Country, [state], City, Pincode)
)AS pvt

Which yields the results
Country         State           City           Pincode
India	        AndhraPradesh	Nellore	        524001
Pakisthan	PakState	Hyderabad	251140
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900