MSSQL - 使用Row_Number在一行中最低和第二低

时间:2014-05-06 17:29:00

标签: sql-server sql-server-2008

我一直致力于查询,为我提供员工的第一和第二审核日期,以及审核结果。下面的查询为我提供了两行(第1和第2行)的信息。但是,我需要将数据显示在一行中。这是我现在的查询:

SELECT EmployeeID,ReviewDate, ReviewResults FROM 
    (
    SELECT EmployeeID, ReviewDate, ReviewResults, MAX(case when RowNumber = 1 then ReviewDate end) as Lowest FROM
        (
        Select EmployeeID, a.ReviewDate, isnull(at.ReviewResults,'Not Completed') as ReviewResults,
        ROW_NUMBER() OVER (PARTITION BY EmployeeID Order By ReviewDate) as RowNumber
        FROM Employee p
        LEFT JOIN Administration a on p.EmployeeID = a.ManagerID
        LEFT JOIN AdminText at on a.ReviewID = at.ReviewID
        GROUP BY p.EmployeeID, a.ReviewDate, at.ReviewResults
        ) as sub_inner
    WHERE ReviewDate>=ISNULL(NULL,'1/1/1900')
        And ReviewDate<dateadd(day,1,ISNULL(NULL,'1/1/3000'))
        AND RowNumber Between 1 and 2
        GROUP BY EmployeeID, ReviewDate, ReviewResults
    ) as sub_outer
    GROUP BY EmployeeID, ReviewDate, ReviewResults

结果如下:

ID        Review Date                   ReviewResults
----      -----------------------       -------------
1         2001-07-05 07:45:00.000         No Change 
1         2001-07-28 10:00:00.000         Raise
2         2001-07-23 10:00:00.000         Promotion 
2         2001-07-28 12:45:00.000         No Change

我想要的是:

ID        Review Date1    Review Date2   ReviewResults1 ReviewResults2
----      -------------   -------------  -------------  --------------
1         2001-07-05      2001-07-28      No Change     Raise
2         2001-07-23      2001-07-28      Promotion     No Change

不确定如何实现这个结果?提前谢谢。

1 个答案:

答案 0 :(得分:2)

测试数据

DECLARE @TABLE TABLE (ID INT, [Review Date] DATETIME, ReviewResults VARCHAR(100))
INSERT INTO @TABLE VALUES
(1,         '2001-07-05 07:45:00.000',         'No Change'), 
(1,         '2001-07-28 10:00:00.000',         'Raise'),
(2,         '2001-07-23 10:00:00.000',         'Promotion'), 
(2,         '2001-07-28 12:45:00.000',         'No Change')

<强>查询

;WITH CTE
AS (
    -- You would use your above query here 
    SELECT ID, [Review Date], ReviewResults
          , ROW_NUMBER() OVER (PARTITION BY ID ORDER BY [Review Date]) rn
    FROM @TABLE 
   )
SELECT C1.ID 
       ,C1.[Review Date]  AS [Review Date 1]
       ,C2.[Review Date]  AS [Review Date 2]
       ,C1.ReviewResults  AS [ReviewResults 1]
       ,C2.ReviewResults  AS [ReviewResults 2]       
FROM CTE C1 INNER JOIN CTE C2
ON C1.ID = C2.ID AND C1.rn +1 = C2.rn

结果集

╔════╦═════════════════════════╦═════════════════════════╦═════════════════╦═════════════════╗
║ ID ║      Review Date 1      ║      Review Date 2      ║ ReviewResults 1 ║ ReviewResults 2 ║
╠════╬═════════════════════════╬═════════════════════════╬═════════════════╬═════════════════╣
║  1 ║ 2001-07-05 07:45:00.000 ║ 2001-07-28 10:00:00.000 ║ No Change       ║ Raise           ║
║  2 ║ 2001-07-23 10:00:00.000 ║ 2001-07-28 12:45:00.000 ║ Promotion       ║ No Change       ║
╚════╩═════════════════════════╩═════════════════════════╩═════════════════╩═════════════════╝