从一个枢轴列双重旋转

时间:2013-12-06 00:00:47

标签: sql-server-2008 tsql pivot

我一直在四处寻找并认为我已经找到了答案的大部分内容。我得到的是一张表格,通过'PayDate'显示员工的薪酬明细,如下所示:

ID   | Name  | Dept  | RateDate  |  HrRate  |  BonusDate |  Bonus  |
--------------------------------------------------------------------
 1   | John  |  100  | 1/1/13 R  |  10.00   |  1/1/13 B  |   200   |
--------------------------------------------------------------------
 1   | John  |  100  | 2/1/13 R  |  10.50   |  2/1/13 B  |   220   |
--------------------------------------------------------------------
 1   | John  |  100  | 3/1/13 R  |  11.00   |  3/1/13 B  |     0   |
--------------------------------------------------------------------
 1   | John  |  100  | 4/1/13 R  |  12.00   |  4/1/13 B  |   250   |
--------------------------------------------------------------------
 2   | Jeff  |  100  | 1/1/13 R  |  15.00   |  1/1/13 B  |   500   |
--------------------------------------------------------------------
 2   | Jeff  |  100  | 2/1/13 R  |  15.00   |  2/1/13 B  |   400   |
--------------------------------------------------------------------
 2   | Jeff  |  100  | 3/1/13 R  |  15.00   |  3/1/13 B  |   500   |
--------------------------------------------------------------------
 2   | Jeff  |  100  | 4/1/13 R  |  15.00   |  4/1/13 B  |   500   |
--------------------------------------------------------------------

我希望得到的是:

ID   | Name  | Dept  |  1/1/13 R  |  1/1/13 B |  2/1/13 R  |  2/1/13 B | etc..
------------------------------------------------------------------------
 1   | John  | 100   |   10.00    |    200    |    10.50   |    220    | etc..
------------------------------------------------------------------------
 1   | Jeff  | 100   |   15.00    |    500    |    15.00   |    400    | etc..
------------------------------------------------------------------------

我试图基本上为每个员工获得一条线,以及他们在给定支付期内的费率和奖金。我正在动态创建列,因此我目前使用的代码如下:

DECLARE @columns1 VARCHAR(8000)
DECLARE @columns2 VARCHAR(8000)
DECLARE @query VARCHAR(8000)

SELECT @columns1 = COALESCE (
            @columns1 + ',[' + CAST(BonusDate as VARCHAR) + ']',
            '[' + CAST(BonusDate as VARCHAR) + ']'
        )
FROM
    #tmptable1
GROUP BY
    BonusDate
ORDER BY
    BonusDate

SELECT @columns2 = COALESCE (
            @columns2 + ',[' + CAST(RateDate as VARCHAR) + ']',
            '[' + CAST(RateDate as VARCHAR) + ']'
        )
FROM
    #tmptable1
GROUP BY
    RateyDate
ORDER BY
    RateDate

SET @query = '
SELECT
    ID
    ,Name
    ,Dept
    ,' + @columns1 + '
    ,' + @columns2 + '
FROM (
    SELECT
        ID
        ,Name
        ,Dept
        ,HrRate
        ,RateDate
        ,Bonus
        ,BonusDate
    FROM
        #tmptable1
    ) as x
PIVOT (
    SUM(Bonus)
    FOR BonusDate
        IN(' + @columns1 + ')
)
AS p1
PIVOT (
    SUM(HrRate)
    FOR RateDate
        IN(' + @columns2 + ')
)
AS p2
'

执行(@query)

这为每个日期(RateDate和BonusDate)提供了一行。所以,我快到了。

支持动态列创建数据透视表脚本的http://www.tsqltutorials.com/

1 个答案:

答案 0 :(得分:0)

试试这个Sql ..

HRRate对员工John有不同的价值,因此为什么它为John提供了多行

DECLARE @columns VARCHAR(8000)
DECLARE @query VARCHAR(8000)

SELECT @columns = COALESCE (
            @columns + ',[' + CAST(PayDate as VARCHAR) + ']',
            '[' + CAST(PayDate as VARCHAR) + ']'
        )
FROM
    #tmptable1
GROUP BY
    PayDate
ORDER BY
    PayDate

set @query = 'SELECT id,name,dept, ' + @columns + ' from 
        (
            select id,name,dept
                , Bonus
                , PayDate
            from #tmptable1
       ) x           

        pivot 
        (
           SUM(Bonus)
            for PayDate in (' + @columns + ')
        ) p1 '

EXECUTE ( @query )