0

I'm trying to create a dynamic pivot table to get results in different columns rather than rows.

This is the table I'm using to test

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 ('TEST','TEST',12345,1,'Giust','Fede')
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12345,2,'Giust','Fede')
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12346,1,'Giust','Fede')
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12346,2,'Giust','Fede')
INSERT INTO Authors (Client_Id, Project_Id, Person_Id, Author_Number, Family_Name, First_Name)
VALUES ('TEST','TEST',12346,3,'Giust','Fede')

So far I get the results like this

CLIENT_ID   PROJECT_ID  PERSON_ID   AUTHOR_NUMBER   FAMILY_NAME FIRST_NAME
TEST        TEST        12345       1               Giust       Fede
TEST        TEST        12345       2               Ma          Ke
TEST        TEST        12346       1               Jones       Peter
TEST        TEST        12346       2               Davies      Bob
TEST        TEST        12346       3               Richards        Craig

I need the results to come out like this, and to be dynamic because sometime I can have 2 authors, or 10 authors.

CLIENT_ID   PROJECT_ID  PERSON_ID   FAMILY_NAME_1   FIRST_NAME_1    FAMILY_NAME_2   FIRST_NAME_2        FAMILY_NAME_3   FIRST_NAME_3      
TEST        TEST        12345       Giust           Fede            Ma              Ke

TEST        TEST        12346       Jones           Peter           Davies          Bob                 Richards            Craig

I've been trying to use this code, but keep getting errors

SQL Fiddle

4

1 回答 1

0

这里有一些建议可以解决您的问题。

首先,您当前的代码正在取消透视表中的所有列,您只需要 UNPIVOTFamily_NameFirst_Name列。因此,我不会使用变量来获取此列列表,只需硬编码您需要取消透视的两列。

然后要获取 PIVOT 的列列表,更改代码以同时使用Author_number和 ,然后使用带有列名Family_Name和的字符串First_Name

select @colsPivot = STUFF((SELECT ',' + QUOTENAME(c.col + '_'+cast(Author_Number as varchar(10))) 
                    from Authors
                    cross apply
                    (
                      select 'Family_Name' col, 1 so union all
                      select 'First_Name', 2
                    ) c
                    group by col, author_number, so
                    order by author_number, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

这给出了一个列列表:

|                                                                                     COLUMN_0 |
------------------------------------------------------------------------------------------------
| [Family_Name_1],[First_Name_1],[Family_Name_2],[First_Name_2],[Family_Name_3],[First_Name_3] |

对您的代码进行这些更改,最终查询将是

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 'Family_Name' col, 1 so union all
                      select 'First_Name', 2
                    ) c
                    group by col, author_number, so
                    order by author_number, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query 
  = 'select Client_id, Project_id, Person_Id, '+@colsPivot+' 
      from
      (
        select Client_id, Project_id, Person_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 (Family_Name, First_Name)
        ) u
      ) x1
      pivot
      (
        max(val)
        for col in ('+ @colspivot +')
      ) p'

exec(@query)

请参阅SQL Fiddle with Demo。这给出了一个结果:

| CLIENT_ID | PROJECT_ID | PERSON_ID | FAMILY_NAME_1 | FIRST_NAME_1 | FAMILY_NAME_2 | FIRST_NAME_2 | FAMILY_NAME_3 | FIRST_NAME_3 |
-----------------------------------------------------------------------------------------------------------------------------------
|      TEST |       TEST |     12345 |         Giust |         Fede |            Ma |           Ke |        (null) |       (null) |
|      TEST |       TEST |     12346 |         Jones |        Peter |        Davies |          Bob |      Richards |        Craig |
于 2013-05-28T14:53:42.790 回答