2

我有一个表,它以常规的分层方式存储公司信息及其母公司,包括 companyid、parentid 和名称。

我只是在 Sql Server 中学习 CTE 查询并编写此查询

WITH tableR (ParentCompanyID, CompanyID, Levels)
AS
(
-- Anchor member definition
    SELECT e.ParentCompanyID, e.CompanyID, 0 As Levels
    FROM tblCompany AS e   
    WHERE ParentCompanyID in (9)
    UNION ALL
-- Recursive member definition
    SELECT e.ParentCompanyID, e.CompanyID, Levels  + 1
    FROM tblCompany AS e   
    INNER JOIN tableR AS d
        ON e.ParentCompanyID = d.CompanyID
)
-- Statement that executes the CTE
SELECT tabler.Levels, tableR.CompanyID, (left('--------------', (tabler.Levels* 2)) + c.CompanyName) as CName,c.ParentCompanyID
FROM tableR  Left join tblcompany c on tableR.CompanyId=c.CompanyID

这很好用,除了它首先列出 ID=9 的子级,然后列出第一级子级,然后列出 2 级 .. 等等,但我需要让子级数据位于其父级之下,所以

L0
  L1
    L2
  L1-1
    L2-1
 ....

有可能吗?因为如果不是,那么我必须在我正在使用的 C# 代码中递归地执行它。

我也试试这个

WITH tableR (ParentCompanyID, CompanyID, Levels, RowNumber)
AS
(
-- Anchor member definition
    SELECT e.ParentCompanyID, e.CompanyID, 1 As Levels, CAST((Row_Number() Over (Order by e.CompanyName) ) as Varchar(MAx)) as RowNumber
    FROM tblCompany AS e   
    WHERE ParentCompanyID in (9)
    UNION ALL
-- Recursive member definition
    SELECT e.ParentCompanyID, e.CompanyID, Levels  + 1, CAST(Concat(d.RowNumber, CAST((Row_Number() Over (Order by e.CompanyName) ) as VARCHAR(MAX)) ) as VARCHAR(MAX)) as RowNumber
    FROM tblCompany AS e   
    INNER JOIN tableR AS d
        ON e.ParentCompanyID = d.CompanyID
)
-- Statement that executes the CTE
SELECT tabler.Levels, RowNumber, tableR.CompanyID, (left('--------------', ((tabler.Levels - 1)* 2 )) + c.CompanyName) as CName,c.ParentCompanyID
FROM tableR  Left join tblcompany c on tableR.CompanyId=c.CompanyID order by RowNumber 

但如果任何级别的记录超过 9 条,它就会失败。

4

4 回答 4

3

试试这个解决方案:

DECLARE @Company TABLE
(
    CompanyID INT PRIMARY KEY,
    Name NVARCHAR(50) NOT NULL,
    ParentCompanyID INT NULL
);
INSERT @Company (CompanyID,Name,ParentCompanyID)
VALUES 
(8,N'Tomaten',NULL),
(9,N'NON ĂNŞI chars',NULL),
(10,N'Bananen',NULL),
(11,N'Child #1',9),
(12,N'Child #2',9),
(13,N'Child #1.1',11),
(14,N'Child #1.2',11);

DECLARE @ParentCompanyID INT = 9;
WITH RecComp
AS
(
    SELECT  crt.CompanyID,
            crt.Name,
            crt.ParentCompanyID,
            1 AS Lvl,
            N'/' + CONVERT(NVARCHAR(4000),crt.CompanyID) + N'/' AS CompanyNode_AsChar
    FROM    @Company crt
    WHERE   crt.ParentCompanyID = @ParentCompanyID
    UNION ALL
    SELECT  cld.CompanyID,
            cld.Name,
            cld.ParentCompanyID,
            prt.Lvl + 1,
            prt.CompanyNode_AsChar + CONVERT(NVARCHAR(4000), cld.CompanyID) + N'/'
    FROM    RecComp prt -- parent
    INNER JOIN @Company cld ON prt.CompanyID = cld.ParentCompanyID
)
SELECT  *,
        CONVERT(HIERARCHYID, CompanyNode_AsChar) AS CompanyNode
FROM    RecComp
ORDER BY CompanyNode;

结果:

CompanyID Name       ParentCompanyID Lvl CompanyNode_AsChar CompanyNode
--------- ---------- --------------- --- --------------------- -----------
11        Child #1   9               1   /11/                  0xAE
13        Child #1.1 11              2   /11/13/               0xAF6C
14        Child #1.2 11              2   /11/14/               0xAF74
12        Child #2   9               1   /12/                  0xB2

注意:SQL Azure 支持 Hierarchyid 数据类型

于 2013-09-12T18:38:10.877 回答
0

好吧,东西 ios,你没有 ORDER BY 子句。

为什么不至少尝试

-- Statement that executes the CTE
SELECT tabler.Levels, tableR.CompanyID, (left('--------------', (tabler.Levels* 2)) + c.CompanyName) as CName,c.ParentCompanyID
FROM tableR  Left join tblcompany c on tableR.CompanyId=c.CompanyID
ORDER BY tableR.Levels

也就是说,显示/UI 部分可能应该由 UI 输出而不是您的查询来处理。

这几乎看起来像是您希望在 TreeView 中显示它。

于 2013-09-12T17:47:53.173 回答
0

好的,我终于找到了解决问题的方法。为了得到批评,如果我在这里帮助别人是对的,那就是

WITH tableR (ParentCompanyID, CompanyID, Levels, RowNumber, RowNumber2)
AS
(
-- Anchor member definition
    SELECT e.ParentCompanyID, e.CompanyID, 1 As Levels, CAST((Row_Number() Over (Order by e.CompanyName) ) as Varchar(MAx)) as RowNumber,
    CAST(
     (Left('000', 3-Len(CAST((Row_Number() Over (Order by e.CompanyName) ) as Varchar(MAx)))) + CAST((Row_Number() Over (Order by e.CompanyName) ) as Varchar(MAx))) 
     As VARCHAR(MAX)
     ) AS RowNumber2
    FROM tblCompany AS e   
    WHERE ParentCompanyID in (370)
    UNION ALL
-- Recursive member definition
    SELECT e.ParentCompanyID, e.CompanyID, Levels  + 1, CAST((Row_Number() Over (Order by e.CompanyName) ) as Varchar(MAx)) as RowNumber1,
    CAST(
        Concat(d.RowNumber2, 
         Left('000', 3-Len(CAST((Row_Number() Over (Order by e.CompanyName) ) as Varchar(MAx)))),
          CAST((Row_Number() Over (Order by e.CompanyName) ) as Varchar(MAx))
        ) as VARCHAR(MAX)) as RowNumber2

    FROM tblCompany AS e   
    INNER JOIN tableR AS d
        ON e.ParentCompanyID = d.CompanyID

)
-- Statement that executes the CTE
SELECT tabler.Levels, RowNumber, RowNumber2,  tableR.CompanyID, (left('--------------', ((tabler.Levels - 1)* 2 )) + c.CompanyName) as CName,c.ParentCompanyID
FROM tableR  Left join tblcompany c on tableR.CompanyId=c.CompanyID order by RowNumber2, CName  

现在,这里是解释:

  1. 我添加了 SQL Server 的 ROW_NUMBER 函数,这只是将计数器添加到查询的每一行,因此它将单独的计数器添加到锚查询和递归查询。
  2. 但是因为我们必须安排它们,所以为了我将父/锚查询值附加到孩子,所以锚查询去 1、2、3.. 但孩子去 11、12 ... 21 ...
  3. 然后我将它们转换为字符串,因为在字符串顺序中你将有 1、2、21、3 而不是 1、2、3、21 .. 所以这对我来说很好。

已知问题:当您点击 Anchor 查询输出 > 10 行或事实上任何超过 10 行的行时,它会被击倒,因为然后锚查询将 ID 设为 11,而 child 为 111 并混淆输出。

上述问题的解决方案:我修改我的查询以使用 LEFT 并附加 000,所以如果我看到我最多可以有 100 个孩子,如果你看到 4,我将 3 设为零,然后使用 0000 并将查询中的 3 更改为 4。

但是:我强烈推荐上面的答案,因为这是这样做的方法。

于 2013-09-12T18:07:26.663 回答
0

我想分享这个

如果你想订购数据......字母和子数据就在它们的父级之下......创建一个baseCTE,使用row_number而不是CompanyID,从Base CTE调用Anchor查询

基本 CTE

ROW_NUMBER() OVER ( PARTITION BY ParentCompanyID ORDER BY CompanyName) as [row_number]

锚点查询

N'/' + CONVERT(NVARCHAR(4000),[row_number]) + N'/' AS CompanyNode_AsChar

递归查询

prt.CompanyNode_AsChar + CONVERT(NVARCHAR(4000), [row_number]) + N'/'
于 2015-07-21T18:54:27.763 回答