将行转换为列

时间:2010-11-24 20:25:35

标签: sql-server sql-server-2005 pivot

我有以下结构:

Emp  PayDate       Amount    
1    11/23/2010    500    
1    11/25/2010    -900    
1    11/28/2010    1000    
1    11/29/2010    2000    
2    11/25/2010    2000    
3    11/28/2010    -3000    
2    11/28/2010    4000    
3    11/29/2010    -5000

如果选择了emp 1,我需要获得以下结果(前3个日期及其对应的val - 如果它们存在 - 第4行总是被忽略)

PayDate1     Amount1   Paydate2     Amount2   Paydate3    Amount3
11/23/2010   500       11/25/2010   -900      11/28/2010  1000

如果选择了emp 2,我需要得到以下结果

Paydate1    Amount1   Paydate2     Amount2   Paydate3 Amount3
11/25/2010  2000      11/28/2010   4000      NULL     NULL

如果选择了emp 3,我需要得到以下结果

Paydate1       Amount1   Paydate2      Amount2   Paydate3   Amount3
11/28/2010    -3000      11/29/2010    -5000

要获取行中的相应数据,我可以运行以下查询:

select top 3 Paydate, Amount from Table where Emp = @Emp

但是我如何以一种转向的方式获得结果呢?

2 个答案:

答案 0 :(得分:1)

有一篇关于使用SQL Server 2005+ here的Pivots的优秀文章。

答案 1 :(得分:0)

CREATE TABLE dbo.Table1 
(
    Emp        int,
    PayDate    datetime,
    Amount     int
)
GO

INSERT INTO dbo.Table1 VALUES (1, '11/23/2010',500)
INSERT INTO dbo.Table1 VALUES (1, '11/25/2010',-900)
INSERT INTO dbo.Table1 VALUES (1, '11/28/2010',1000)
INSERT INTO dbo.Table1 VALUES (1, '11/29/2010',2000)
INSERT INTO dbo.Table1 VALUES (2, '11/25/2010',2000)
INSERT INTO dbo.Table1 VALUES (3, '11/28/2010',-3000)
INSERT INTO dbo.Table1 VALUES (2, '11/28/2010',4000)
INSERT INTO dbo.Table1 VALUES (3, '11/29/2010',-5000)


;WITH cte AS
(SELECT Emp, PayDate, Amount, PayDateRowNumber
FROM 
(SELECT Emp,
       PayDate,
       Amount,
       ROW_NUMBER() OVER (PARTITION BY Emp ORDER BY PayDate) AS PayDateRowNumber
FROM Table1) AS RankedTable1
WHERE PayDateRowNumber < 4)
SELECT c1.Emp AS Emp, c1.PayDate AS PayDate1
        ,c1.Amount AS Amount1, c2.PayDate AS PayDate2
        ,c2.Amount AS Amount2, c3.PayDate AS PayDate3, c3.Amount AS Amount3
FROM cte c1
LEFT JOIN cte c2 ON c2.Emp = c1.Emp AND c2.PayDateRowNumber = 2
LEFT JOIN cte c3 ON c3.Emp = c2.Emp AND c3.PayDateRowNumber = 3
WHERE c1.PayDateRowNumber = 1

输出是:

alt text

有些警告是,它不会汇总同一雇主/日期的金额(虽然很容易改变)。也可能想要更改以查看ROW_NUMBER()与RANK()和DENSE_RANK()的使用,具体取决于您对“TOP 3”的定义