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.
;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
;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