Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to turn related records into hierarchical recordset

$
0
0

Hello,

I have two tables of data and the data is related. I however need it to be heirarchical for binding to a tree control and in this data set it is probable that child records will have more than one parent. So I was unsure how to write such a query so I posted this question: http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/2e3987a3-093e-412e-a771-e4b1395d8814.  Based upon this understanding I now have three tables:

DECLARE @Foods TABLE(FoodNameID INT,FoodName VARCHAR(50)) 
INSERT INTO @Foods VALUES(56743, 'Omlette')
INSERT INTO @Foods VALUES(56756, 'Yogurt')
INSERT INTO @Foods VALUES(56735, 'Filet Mignon')

DECLARE @FoodGroups TABLE(FoodGroupID INT,FoodGroupName VARCHAR(50))
INSERT INTO @FoodGroups VALUES(1001, 'Breakfast')
INSERT INTO @FoodGroups VALUES(1007, 'High Protein')
INSERT INTO @FoodGroups VALUES(1009, 'Fresh')
INSERT INTO @FoodGroups VALUES(1002, 'Red Meat')

DECLARE @Foods_FoodGroups TABLE(FoodListID INT, FoodParentID INT, FoodNameID INT, FoodGroupID INT)

INSERT INTO @Foods_FoodGroups VALUES(1, null, null, 1001)
INSERT INTO @Foods_FoodGroups VALUES(2, 1, 56743, 1001)
INSERT INTO @Foods_FoodGroups VALUES(3, null, null, 1007)
INSERT INTO @Foods_FoodGroups VALUES(4, 3, 56743, 1007)
INSERT INTO @Foods_FoodGroups VALUES(5, null, null, 1009)
INSERT INTO @Foods_FoodGroups VALUES(6, 5, 56743, 1009)
INSERT INTO @Foods_FoodGroups VALUES(7, 1, 56756, 1001)
INSERT INTO @Foods_FoodGroups VALUES(8, 3, 56756, 1007)
INSERT INTO @Foods_FoodGroups VALUES(9, null, null, 1002)
INSERT INTO @Foods_FoodGroups VALUES(10, 9, 56735, 1002);

One thing that jumps out at me right away is that I have to enter "dummy records" for the category groups like the first record 1, null, null, 1001. This just seems wrong so I am highly suspicious of the monster I am creating.

I can write a cte such as this but it is not exactly ideal because it has to deal with the questionable "join / hierarchy table above.

WITH FoodList AS
 (
	SELECT
		ParentFFG.FoodListID,
		ParentFFG.FoodNameID,
		ParentFFG.FoodGroupID,
		ParentFFG.FoodParentID
	FROM 
		@Foods_FoodGroups ParentFFG
	WHERE 
		ParentFFG.FoodParentID IS NULL
	UNION ALL
	SELECT 
		FFG.FoodListID,
		FFG.FoodNameID,
		FFG.FoodGroupID,
		FFG.FoodParentID
	FROM 
		@Foods_FoodGroups FFG
	INNER JOIN FoodList AS FL ON
	FFG.FoodParentID = FL.FoodListID
	WHERE FFG.FoodParentID IS NOT NULL
 )
SELECT 
	Flst.*,
	Fgp.FoodGroupName,
	Fds.FoodName
FROM 
	FoodList Flst
INNER JOIN @FoodGroups Fgp ON
Flst.FoodGroupID = Fgp.FoodGroupID

LEFT JOIN @Foods Fds ON
Fds.FoodNameID = Flst.FoodNameID

So I need some direction on how to properly structure my tables and or queries as this related hierarchy scenario will happen multiple times.

Thank You

JB


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>