使用动态列值进行旋转

时间:2015-03-30 09:07:17

标签: sql-server-2008 pivot

我试图使用枢轴从桌子上获取Lenoir记录,但无法获得。

我的表格标签:

id  TaskID  TaskTitle AssignedTo    Priority Closed ACTID
1   9   Task1           User1   Normal   1  3
2   13  Task2           User2   Normal   3  3
3   14  Task3           User3   High     1  3
4   15  Task3           User3   Normal   3  4
5   16  Task1           User1   High     2  5
6   17  Task2           User2   High     3  5
7   19  Task3           User3   Normal   1  5
8   9   Task4           User4   Normal   3  5

我尝试使用枢轴但无法获得

我所需的输出如下:

ActID   Task1_TaskTitle Task1_AssignedTo    Task1_Priority  Task1_Closed    Task2_TaskTitle Task2_AssignedTo    Task2_Priority  Task2_Closed    Task2_TaskTitle Task3_AssignedTo    Task3_Priority  Task3_Closed    Task4_TaskTitle Task4_AssignedTo    Task4_Priority  Task4_Closed
3    Task1  User1   Normal  1    Task2  User2   Normal  3    Task3  User3   High    1               
4    Task3  User3   Normal  3                                               
5    Task1  User1   High    2    Task2  User2   High    3    Task3  User3   Normal  1    Task4  User4   Normal  3

如何获得这些?

1 个答案:

答案 0 :(得分:0)

它可能对您有用。

Declare  @Total         Int
        ,@i             Int
        ,@Sql           Varchar(8000)
        ,@TaskTitle     Varchar(100)

Select  @Sql = ''

If Object_Id('tempdb.dbo.#test') Is Not Null
Begin
    Drop Table #test;
End

If Object_Id('tempdb.dbo.#labels') Is Not Null
Begin
    Drop Table #labels;
End

If Object_Id('tempdb.dbo.#final') Is Not Null
Begin
    Drop Table #final;
End

If Object_Id('tempdb.dbo.#test') Is Null
Begin
    Create Table #test
    (
            Id              Int Primary Key
        ,TaskId         Int
        ,TaskTitle      Varchar(100)
        ,AssignedTo     Varchar(100)
        ,Priority       Varchar(100)
        ,Closed         Int
        ,ActId          Int
    )
End

If Object_Id('tempdb.dbo.#labels') Is Null
Begin
    Create Table #labels
    (
            Id              Int Identity(1,1) Primary Key
        ,Label          Varchar(100)
    )
End

If Object_Id('tempdb.dbo.#final') Is Null
Begin
    Create Table #final
    (
        ActId           Int Primary Key
    )
End


Insert Into #test(Id,TaskId,TaskTitle,AssignedTo,Priority,Closed,ActId) Values
    (1,9,'Task1','User1','Normal',1,3)
,(2,13,'Task2','User2','Normal',3,3)
,(3,14,'Task3','User3','High',1,3)
,(4,15,'Task3','User3','Normal',3,4)
,(5,16,'Task1','User1','High',2,5)
,(6,17,'Task2','User2','High',3,5)
,(7,19,'Task3','User3','Normal',1,5)
,(8,9,'Task4','User4','Normal',3,5);

---- distinct task title
Insert Into #labels(Label)
Select  Distinct
        t.TaskTitle
From    #test As t With (Nolock)
Order By t.TaskTitle

---- prepared final result
Insert Into #final(ActId)
Select  Distinct
        t.ActId
From    #test As t With (Nolock)

Select   @Total = Count(1)
        ,@i = 1
        ,@Sql = 'Alter Table #final Add '
From    #labels As l With (Nolock)

---- altered final result table
While (@i <= @Total)
Begin
    Select  @TaskTitle =   l.Label
    From    #labels As l With (Nolock)
    Where   l.Id = @i

    Select  @Sql = @Sql + (Case When @i > 1 Then ',' Else '' End) + @TaskTitle + '_TaskTitle Varchar(100), ' 
        + @TaskTitle + '_AssignedTo Varchar(100), ' 
        + @TaskTitle + '_Priority Varchar(100), ' 
        + @TaskTitle + '_Closed Int'

    Select @i = @i + 1
End

----Print (@Sql)
Exec (@Sql)

---- reset counter
Select  @i = 1

---- update final result table with valid data.
While (@i <= @Total)
Begin
    Select  @TaskTitle =   l.Label
    From    #labels As l With (Nolock)
    Where   l.Id = @i

    Select  @Sql =  'Update f ' +
                    'Set      f.'+ @TaskTitle + '_TaskTitle = t.TaskTitle ' +
                            ',f.'+ @TaskTitle + '_AssignedTo = t.AssignedTo ' +
                            ',f.'+ @TaskTitle + '_Priority = t.Priority ' +
                            ',f.'+ @TaskTitle + '_Closed = t.Closed ' +
                    'From   #final As f ' +
                            'Join #test As t With (Nolock) On f.ActId = t.ActId ' +
                    'Where  t.TaskTitle = ''' + @TaskTitle + ''' ;'
    ----Print (@Sql)
    Exec (@Sql)

    Select   @i = @i + 1
            ,@Sql = ''
End

---- final result
Select  *
From    #final As f With (Nolock)