这是一个很好的谜题,得告诉你:) 希望它是你想要它做的。
DECLARE @GroupRootID INT = 1
DECLARE @GROUP_MEMBERS TABLE (ID int, CHILDTYPE int, CHILDID int)
DECLARE @ITEMS TABLE (ID int, STATUS int)
INSERT INTO @GROUP_MEMBERS VALUES
(1,0,2), (1,1,1), (2,1,2), (2,0,3), (2,0,4), (2,1,3), (2,1,4), (3,1,5), (4,1,5)
INSERT INTO @ITEMS VALUES
(1,0), (2,1), (3,0), (4,0), (5,1)
-- 1
-- / \
-- 2 items items: 1 => 1,1,1
-- /| \
-- 3 4 items items: 2,3,4 => 2,1,2 - 2,1,3 - 2,1,4
-- | \
-- items items items (3): 5 => 3,1,5
-- items (4); 5 => 4,1,5
/* Recursivly build the GROUP tree (groups that have subgroups, CHILDTYPE=0), but NOT the lead nodes (CHILDTYPE = 1) */
;WITH GROUP_TREE
AS
(
/* SELECT all parents */
SELECT ParentGroups.*, 0 AS LEVEL
FROM @GROUP_MEMBERS AS ParentGroups
WHERE ParentGroups.CHILDTYPE = 0
AND ParentGroups.ID = @GroupRootID
UNION ALL
/* SELECT all childs groups for the parents */
SELECT ChildGroups.*, LEVEL + 1
FROM @GROUP_MEMBERS AS ChildGroups
INNER JOIN GROUP_TREE AS Parent ON Parent.CHILDID = ChildGroups.ID
WHERE ChildGroups.CHILDTYPE = 0
)
/* We now have all groups with their subgroups (not leaf nodes) */
/* Then join the leaf nodes (groups that are no subtree) */
/* Finally union the items from the root node and join the ITEMS to the leaf nodes to get the status */
/* Mind you though that ITEM 5 is linked double and will be returned NON-distinct */
SELECT ITEMS.*
FROM (
SELECT GROUPS.*
FROM @GROUP_MEMBERS AS GROUPS
INNER JOIN GROUP_TREE ON GROUP_TREE.CHILDID = GROUPS.ID
WHERE GROUPS.CHILDTYPE = 1
UNION ALL
SELECT GROUPS.*
FROM @GROUP_MEMBERS AS GROUPS
WHERE GROUPS.CHILDTYPE = 1
AND GROUPS.ID = @GroupRootID
) AS GROUP_ITEMS
INNER JOIN @ITEMS AS ITEMS ON GROUP_ITEMS.CHILDID = ITEMS.ID
您是否需要每个组的ITEMS(如表值函数)或所有组的ITEMS的总列表? – martennis
另外,你是否只有1级递归,或者更多? – martennis
可能有超过1级的递归 - 我更愿意为每个组的ITEMS获得一个TVF,我认为。我想对返回的状态执行一些数学运算。 – user1695263