recursive sql function with rollup logic?

后端 未结 3 830
感情败类
感情败类 2021-01-28 23:33

i have a SQL that using a recursive CTE to expand a self-referancing employees table builds a result set of defects aggregated by user and severity level.

here is my CTE

3条回答
  •  暖寄归人
    2021-01-29 00:23

    here is the long dummy way of doing it. i have it working but the solution could be much better. i am hoping someone will post a SQL2005 way of getting this done...

        alter PROC sel_DefectReportByManagerNTID_rollup
    (@ManagerNTID NVARCHAR(100))    
    AS
    
    CREATE TABLE #DefectCounts
    (
    id INT IDENTITY(1, 1) ,
    MgrRolledInto NVARCHAR(100) NULL,
    AltBusinessSeverity NVARCHAR(100) NULL,
    DefectCount INT NULL
    );
    
    
    CREATE TABLE #directReports
    (
    pk INT IDENTITY(1, 1) ,
    directReportNTID NVARCHAR(100) NULL
    );
    
    INSERT INTO #directReports
    SELECT NTID FROM Employees WHERE ManagerNTID = @ManagerNTID
    --select * from #directReports
    
    DECLARE @maxPK INT;
    SELECT @maxPK = MAX(PK) FROM #directReports
    
    DECLARE @pk INT;
    SET @pk = 1
    
    
    INSERT INTO #DefectCounts (MgrRolledInto,AltBusinessSeverity,DefectCount)
    SELECT @ManagerNTID, d.AltBusinessSeverity, COUNT(*)
            FROM Defects d
                JOIN StatusCode C ON C.CodeName = d.Status AND c.scid = 10
            WHERE d.AssignedTo = @ManagerNTID
            GROUP BY d.AltBusinessSeverity
    
    
    WHILE @pk <= @maxPK
    BEGIN
        /* Get one direct report at a time to aggregate their defects under them... */
        DECLARE @dirRptNTID NVARCHAR(100);
        SET @dirRptNTID = (SELECT directReportNTID
                            FROM #directReports
                            WHERE PK = @pk)
    
    
        INSERT INTO #DefectCounts (MgrRolledInto,AltBusinessSeverity,DefectCount)
            SELECT @dirRptNTID, d.AltBusinessSeverity, COUNT(*)
            FROM Defects d
                JOIN StatusCode C ON C.CodeName = d.Status AND c.scid = 10
                JOIN (SELECT * FROM fnGetEmployeeHierarchyByUsername(@dirRptNTID) ) emp ON emp.NTID = d.AssignedTo
            WHERE d.AssignedTo IS NOT NULL
            GROUP BY d.AltBusinessSeverity
    
        SELECT @pk = @pk + 1
    END
    
    
    
    SELECT  e.FullName,     
      isnull(Urgent,0) as Urgent,     
      isnull(High,0) as High,     
      isnull(Medium,0) as Medium,    
      isnull(Medium3000,0) as Medium3000,    
      isnull(Low,0) as Low    
    FROM (  select * from fnGetEmployeeHierarchyByUsername (@ManagerNTID) where depth <= 1) e    
    left outer join (
                        SELECT  MgrRolledInto,    
                                SUM([1-Urgent]) AS Urgent,    
                                SUM([2-High]) AS High,    
                                SUM([3-Medium]) AS Medium,    
                                SUM([3-Medium (3000)]) AS Medium3000,  
                                SUM([4-Low]) AS Low    
                        FROM #DefectCounts dfs
                        PIVOT 
                        (sum(DefectCount) FOR AltBusinessSeverity IN ([1-Urgent],[2-High],[3-Medium],[3-Medium (3000)],[4-Low])) V    
                        GROUP BY MgrRolledInto
                    ) def_data on def_data.MgrRolledInto = e.NTID
    order by e.depth
    

提交回复
热议问题