带有两列的pivot组合了ms sql

时间:2014-04-24 05:38:21

标签: sql

我想提出一个结果,以透视模式显示表格中的值。但是,其他行的列名是Person_Id和Author_Number的组合,该列的值应为Project_Id。为了便于理解,请参阅以下示例结果:

Client_Id  12345_1   12345_2  12346_1  12346_2 12346_1
Client1     Test1      Test1   
Client2                       Test2    Test2  
Client3                                        Test3   

请注意,第二列是Person_Id和Author_Number的组合,依此类推。

以下是示例表

CREATE TABLE [dbo].[Authors](
        [Client_Id] [nvarchar](50) NOT NULL,
        [Project_Id] [nvarchar](50) NOT NULL,
        [Person_Id] [int] NOT NULL,
        [Author_Number] [int] NOT NULL,
        [Family_Name] [nvarchar](50) NULL,
        [First_Name] [nvarchar](50) NULL,
    )

    INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
    VALUES ('Client1','TEST1',12345,1,'Giust','Fede')
    INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
    VALUES ('Client1','TEST1',12345,2,'Ma','Ke')
    INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
    VALUES ('Client2','TEST2',12346,1,'Jones','Peter')
    INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
    VALUES ('Client2','TEST2',12346,2,'Davies','Bob')
    INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
    VALUES ('Client3','TEST3',12346,3,'Richards','Craig')

这是脚本

DECLARE @colsPivot AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @colsPivot = STUFF((SELECT ',' + QUOTENAME(c.col + '_'+cast(Author_Number as varchar(10))) 
                    from Authors
                    cross apply
                    (
                       select CAST(Person_Id AS VARCHAR(10)) col 
                    ) c

            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query 
  = 'select Client_id,  '+@colsPivot+' 
      from
      (
        select Client_id,   
          col+''_''+cast(Author_Number as varchar(10)) col, val
        from
        (
          select Client_id, Project_id,
            Person_Id, 
            Author_Number, 
            Family_Name, 
            First_Name
          from Authors
        ) s
        unpivot
        (
          val
          for col in (Project_id)
        ) u
      ) x1
      pivot
      (
        max(val)
        for col in ('+ @colspivot +')
      ) p'

PRINT @query
exec(@query)

我无法弄清楚脚本的问题。客户端ID列之后显示的值为空

1 个答案:

答案 0 :(得分:2)

尝试此查询。我更新了它。

DECLARE @colsPivot AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX)

select @colsPivot = STUFF((SELECT ',' + QUOTENAME(c.col + '_'+cast(Author_Number as varchar(10))) 
                from Authors
                cross apply
                (
                   select CAST(Person_Id AS VARCHAR(10)) col 
                ) c

        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')


set @query   = 'select Client_id,  '+@colsPivot+' 
  from
  (
    select Client_id,   
      cast(Person_id as varchar(10))+''_''+cast(Author_Number as varchar(10)) col, val
    from
    (
      select Client_id, Project_id,
        Person_Id, 
        Author_Number, 
        Family_Name, 
        First_Name
      from Authors
    ) s
    unpivot
    (
      val
      for col in (Project_id)
    ) u
  ) x1
  pivot
  (
    max(val)
    for col in ('+ @colspivot +')
  ) p'

PRINT @query
exec(@query)

以上查询的输出为:

Client_id   12345_1 12345_2 12346_1 12346_2 12346_3
----------------------------------------------------------
Client1          TEST1  TEST1   NULL    NULL    NULL
Client2          NULL   NULL    TEST2   TEST2   NULL
Client3          NULL   NULL    NULL    NULL    TEST3