1

我有一个表示树结构的表 tbltestingtree,Member_Id 是成员的 id,Parent_Id 表示该成员的 Parentid,Lefts 和 Rights 告诉我们该成员附加到 Parent 的左侧或右侧。 我的问题是我必须创建一个 sqlquery 来计算任何特定父级的 pared 成员总数,

id 为 1 的前成员有 3 对 id 为 2,3 和 4
当前 id 为 2 的成员有 1 对 id 为 4

  Member_Id     Parent_Id      Lefts         Rights
     1               Null       Null          Null  
     2                1          1            Null
     3                1         Null           1
     4                2          1            Null
     5                2         Null           1
     6                3          1            Null
     7                3         Null           1
     8                4          1            Null
     9                4         Null           1

树的图像在这里: http://imageshack.us/content_round.php?page=done&l=img4/4605/ 65ai.jpg

4

2 回答 2

0

我认为这是您要查找的查询:

WITH children AS (
  SELECT Parent_Id, COUNT(Parent_Id) AS NR_CHILDREN
  FROM TREE
  GROUP BY Parent_Id
  HAVING COUNT(Parent_Id) >= 2
  )
SELECT T.Parent_Id AS 'PARENT', T.Member_Id AS 'CHILDREN'
FROM TREE T JOIN
  CHILDREN C ON T.Member_Id = C.Parent_Id
WHERE ISNULL(T.Parent_Id, 0) <> 0

这是带有代码的SQLFiddle

于 2013-10-03T12:10:22.757 回答
0
        CREATE TABLE [dbo].[tblTestingTree](
            [Id] [int] IDENTITY(1,1) NOT NULL,
            [ParentId] [int] NULL,
            [IsLeft] [bit] NULL,
            [IsRight] [bit] NULL,
         CONSTRAINT [PK_tblTestingTree] PRIMARY KEY CLUSTERED 
        (
            [Id] ASC
        )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        ) ON [PRIMARY]
        GO
        SET IDENTITY_INSERT [dbo].[tblTestingTree] ON
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (1, NULL, NULL, NULL)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (2, 1, 1, NULL)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (3, 1, NULL, 1)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (4, 2, 1, NULL)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (5, 2, NULL, 1)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (6, 3, 1, NULL)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (7, 3, NULL, 1)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (8, 4, 1, NULL)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (9, 4, NULL, 1)
        INSERT [dbo].[tblTestingTree] ([Id], [ParentId], [IsLeft], [IsRight]) VALUES (10, 5, 1, NULL)
        SET IDENTITY_INSERT [dbo].[tblTestingTree] OFF

declare @ParentId as int
set @ParentId=2 

;with Child as
(
    select id,ParentId from tblTestingTree where id=@ParentId
    union all
    Select tblTestingTree.Id,tblTestingTree.parentId from tblTestingTree 
    inner join Child 
    on tblTestingTree.ParentId=Child.Id
)
select count(*) from (select c.ParentId from Child c where c.ParentId!=@ParentId
group by c.ParentId
having COUNT(c.ParentId)>1)a
于 2013-10-04T11:57:46.447 回答