Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Dear experts,
I have a hierarchical table which stores data like below in SQL Server 2008 database.
C#
ID	Description	Category	ParentID
1	ABC	       Objective	 NULL
2	CDE	       Objective	 NULL
3	XXX	       Objective	 NULL
4	YYY	       Criteria	     1
5	DDD	       Criteria	     1
6	AAA	       Criteria      2
7	CCC	       Procedure     4
8	EEEE	   Procedure     4
9	JJJJ	   Procedure     6

Now, I want to write a Stored procedure to retrieve the data from above Hierarchical Table in the format below (fig. 2):
HTML
Objective	Criteria	Procedure
ABC	         YYY	     CCC
ABC	         YYY	     ZZZ
CDE	         KKK	     RRRR
CDE	         DDD	     EEEE
XXX	         AAA	     JJJJ

for the purpose of REPORT.

EDITED:
I did a CTE Query Like below:
SQL
;WITH CteDAP ( ID, HierarchyID, Description, Category_Level, Depth)
	 AS
	 (
			SELECT DISTINCT 
					  A.ID, A.HierarchyID, A.Description, B.Name AS Category_Level,0 AS Depth
			FROM      tblHierarchy A INNER JOIN tblCat_lvl B ON A.fkCat_lvlId = B.pkCat_lvlId 
			WHERE     A.HierarchyID IS NULL
		UNION ALL 		
			SELECT    
					  C.ID, C.HierarchyID, C.Description, D.Name AS Category_Level, (CTE.Depth + 1) AS Depth
			FROM      tblHierarchy C INNER JOIN tblCat_lvl D ON C.fkCat_lvlId = D.pkCat_lvlId INNER JOIN
				  CteDAP CTE ON C.HierarchyID = CTE.ID
	 )
	 SELECT * FROM CteDAP ORDER BY Depth, Description


Got Result as :
C#
ID HierarchyID	  Description	                    category_Level	    Depth
10	NULL	  Capital Grants…………	                  AOC	            0
11	NULL	  Contributions…………..	                  AOC	            0
15	NULL	  Current Grants …………	                  AOC	            0
20	10	      To ascertain that expenditure…………….	 Overall Objective	1
25	11	      To ascertain that other ……………………….	 Overall Objective	1
26	15	      To ascertain that pay ………………..	     Overall Objective	1
28	20	      To ascertain that …………….	             Sub Objective	    2
30	20	      Ascertain…………..	                     Sub Objective	    2
40	25	      Evaluation of …………….	                 Sub Objective	    2
42	28	      Received should ………..	                 Criteria	        3
43	28	      Received should be ………..	             Criteria	        3
45	40	      All supplies of …………	                 Criteria	        3
50	40	      Annual increments ………………	             Criteria	        3
53	42	      Check from the …………..	                 Procedure	        4
54	42	      Check whether ……………	                 Procedure	        4
59	50	      Check whether it………………	             Procedure	        4
Now I want the data in (fig. 2) format.
Any help will be highly appreciated. Thanks.

What I have tried:

I tried writing CTE but i could not achieve the format in what i wanted, dynamic POVIT may help me but don't know how to write a dynamic POVIT. Help Please!
Posted
Updated 25-Aug-16 22:07pm
v4

Your sample data doesn't make any sense - objective 3 has no criteria, and criteria 5 has no procedure.

Assuming that's just a typo in your question, something like this should work:
SQL
SELECT
    O.Description As Objective,
    C.Description As Criteria,
    P.Description As [Procedure]
FROM
    YourTable As O
    LEFT JOIN YourTable As C
    ON C.ParentID = O.ID And C.Category = 'Criteria'
    LEFT JOIN YourTable As P
    ON P.ParentID = C.ID And P.Category = 'Procedure'
WHERE
    O.ParentID Is Null
;

With your sample data, that produces the following output:
Objective    Criteria    Procedure
ABC          YYY         CCC  
ABC          YYY         EEEE 
ABC          DDD         <NULL>
CDE          AAA         JJJJ 
XXX          <NULL>      <NULL>
 
Share this answer
 
Comments
CringTee 18-Aug-16 23:19pm    
Since it is a Hierarchy Table there will be n number of child or there may not be a child. The data in my question is just a sample data that may help to understand the question. n number of child data will have a level name(category) like in my example "objective, criteria, procedure, sub-procedure, etc". thus, we cannot do "ON C.ParentID = O.ID And C.Category = 'Criteria'", it should be dynamic.
Richard Deeming 19-Aug-16 8:33am    
My answer already deals with the case where one objective has multiple criteria, and where one criteria has multiple procedures.

If that's not what you want, then you need to update your question with a clear and precise description of what you do want. Provide a clear sample of the input, the output, and how the two are related.

Remember, we can't see your screen, access your computer, or read your mind. If you don't tell us precisely what you want, then we can't help you.
CringTee 22-Aug-16 2:30am    
Thanks Sir. I have updated the question as u recommended, If it has a fixed number of child data then yes your solution will help to solve the problem. But my data will have N number of child.
Resolved using CTE and by referring Dynamic Pivot Query in SQL Server[^] "Dynamic Pivot Query in SQL Server"
 
Share this answer
 

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