English 中文(简体)
2005年——共同表格表达——最后是等级结构
原标题:SQL 2005 - Common Table Expression - Find last in hierarchy
  • 时间:2010-11-17 17:41:11
  •  标签:
  • tsql

我要说的是:

CREATE TABLE Employees
(
EmployeeId int PRIMARY KEY NOT NULL,
ParentEmployeId int REFERENCES Employees(EmployeeId) NULL,
Name varChar(255)
)

所有记录都有一个主要识别资料,记录能够把另一个记录作为父母。 (My actual schema isn t about Workers, this is only an brief edition for example so if You ve got a better way of Hand original information it not germane to this dialogue.)

插入以下记录:

INSERT INTO Employees VALUES (1, NULL,  Company President 1 )
INSERT INTO Employees VALUES (2, NULL,  Company President 2 )

INSERT INTO Employees VALUES (3, 1,  Company President 1 - VP )
INSERT INTO Employees VALUES (4, 2,  Company President 2 - VP )

INSERT INTO Employees VALUES (5, 3,  Company President 1 - VP - Secretary )
INSERT INTO Employees VALUES (6, 4,  Company President 2 - VP - Secretary )

INSERT INTO Employees VALUES (7, 5,  Company President 1 - VP - Secretary - Sandwich Delivery )

这些词语代表:

Company President 1
    Company President 1 - VP
        Company President 1 - VP - Secretary
            Company President 1 - VP - Secretary - Sandwich Delivery
Company President 2
    Company President 2 - VP
        Company President 2 - VP - Secretary

我试图做的是,所有拥有全国人民力量联盟的员工。 父母就业情况 我想找到链中的最后一人,例如“。 公司总裁 1 - VP - Secretary -WEPliver和“ Company President 2 - VP - Secretary

下面的CTE向我提供一切东西,包括nes,但我不敢肯定会从这里去。 如果有可能,我想避免治疗。

而且,这是非常重要的,我在其他地方有逻辑,保证雇员只能有1个直属关系。 因此,尽管该表在技术上允许,但<代码>Company President 1将永远不会有两种千兆字节。

WITH EmployeeRec(EmployeeId, ParentEmployeeId, Name, Level) AS
(
    SELECT
        EmployeeId,
        ParentEmployeId,
        Name,
        1 as [Level]
    FROM
        Employees
    WHERE
        ParentEmployeId IS NULL

    UNION ALL

    SELECT
        E.EmployeeId,
        E.ParentEmployeId,
        E.Name,
        R.[Level] + 1
    FROM
        Employees E
    INNER JOIN
        EmployeeRec R
    ON
        E.ParentEmployeId = R.EmployeeId
)
SELECT * FROM EmployeeRec
最佳回答

跟踪你的总经理,使你能够与最后一级一样,保留你所需要的记录。

WITH EmployeeRec(Master, EmployeeId, ParentEmployeeId, Name, Level) AS
(
    SELECT
        [Master] = EmployeeId,
        EmployeeId,
        ParentEmployeId,
        Name,
        1 as [Level]
    FROM
        Employees
    WHERE
        ParentEmployeId IS NULL

    UNION ALL

    SELECT
        R.Master,
        E.EmployeeId,
        E.ParentEmployeId,
        E.Name,
        R.[Level] + 1
    FROM
        Employees E
    INNER JOIN
        EmployeeRec R
    ON
        E.ParentEmployeId = R.EmployeeId
)
SELECT  *
FROM    EmployeeRec er
        INNER JOIN (
          SELECT  Master, Level = MAX(Level)
          FROM    EmployeeRec
          GROUP BY Master
        ) m ON m.Master = er.Master
               AND m.Level = er.Level
问题回答

The key here is to keep Line of the high-level parent in the recursive CTE:

;WITH EmployeeRec(
   EmployeeId, ParentEmployeeId, UltimateGrandbossEmployeeId, Name, Level)
 AS
(
    SELECT
        EmployeeId,
        ParentEmployeeId,
        EmployeeId UltimateGrandbossEmployeeId,
        Name,
        1 as [Level]
    FROM
        Employees
    WHERE
        ParentEmployeeId IS NULL

    UNION ALL

    SELECT
        E.EmployeeId,
        E.ParentEmployeeId,
        R.UltimateGrandbossEmployeeId,
        E.Name,
        R.[Level] + 1
    FROM
        Employees E
    INNER JOIN
        EmployeeRec R
    ON
        E.ParentEmployeeId = R.EmployeeId
)

......组成一个中型企业,以掌握最底层的水平......

SELECT 
    UltimateGrandbossEmployeeId,
    Name,
    ROW_NUMBER() OVER (PARTITION BY UltimateGrandbossEmployeeId 
                       ORDER BY Level Desc ) BottomUp
 FROM EmployeeRec
)

......选择最深的孩子:

SELECT
    UltimateGrandbossEmployeeId, DeepestChildName
FROM
    Inter
WHERE
    BottomUp = 1

(将所有这些代码碎块合在一起,形成与两个电子计算器和一个<代码>SlectT的单一查询。)

成果:

1   Company President 1 - VP - Secretary - Sandwich Delivery
2   Company President 2 - VP - Secretary

您可在JOIN上查阅,以便按您的实际情况判断,获得大豆名称或履历。





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签