How to convert row values to columns with dynamic columns count?

前端 未结 1 2064
囚心锁ツ
囚心锁ツ 2020-12-18 11:19

I have the following data:

DECLARE @DataSource TABLE
(
     [ColumnA] INT
    ,[ColumnB] INT
    ,[ColumnC] INT
)

INSERT INTO @DataSource ([ColumnA], [Colum         


        
相关标签:
1条回答
  • 2020-12-18 11:46

    My suggestion whenever you are working with PIVOT is to alway write the query first with the values hard-coded, then you can easily convert the query to a dynamic solution.

    Since you are going to have multiple values of columnC that will be converted to columns, then you need to look at using the row_number() windowing function to generate a unique sequence for each columnc based on the values of columnA and columnB.

    The starting point for your query will be:

    select [ColumnA],
      [ColumnB],
      [ColumnC],
      'SampleTitle'+
      cast(row_number() over(partition by columna, columnb
                              order by columnc) as varchar(10)) seq
    from DataSource;
    

    See Demo. This query will generate the list of new columns names SampleTitle1, etc:

    | COLUMNA | COLUMNB | COLUMNC |          SEQ |
    |---------|---------|---------|--------------|
    |    5060 |    1006 |  100118 | SampleTitle1 |
    |    5060 |    1006 |  100119 | SampleTitle2 |
    |    5060 |    1006 |  100120 | SampleTitle3 |
    

    You can then apply the pivot on columnC with the new column names listed in seq:

    select columnA, columnB, 
      SampleTitle1, SampleTitle2, SampleTitle3
    from
    (
       select [ColumnA],
        [ColumnB],
        [ColumnC],
        'SampleTitle'+
          cast(row_number() over(partition by columna, columnb
                                  order by columnc) as varchar(10)) seq
       from DataSource
    ) d
    pivot
    (
      max(columnc)
      for seq in (SampleTitle1, SampleTitle2, SampleTitle3)
    ) piv;
    

    See SQL Fiddle with Demo.

    Once you have the correct logic, you can convert the data to dynamic SQL. The key here is generating the list of new column names. I typically use FOR XML PATH for this similar to:

    select STUFF((SELECT distinct ',' + QUOTENAME(seq) 
                    from
                    (
                      select 'SampleTitle'+
                        cast(row_number() over(partition by columna, columnb
                                                order by columnc) as varchar(10)) seq
                      from DataSource
                    ) d
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
    

    See Demo. Once you have the list of column names, then you will generate your sql string to execute, the full code will be:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(seq) 
                        from
                        (
                          select 'SampleTitle'+
                            cast(row_number() over(partition by columna, columnb
                                                    order by columnc) as varchar(10)) seq
                          from DataSource
                        ) d
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT columnA, ColumnB,' + @cols + ' 
                 from 
                 (
                   select [ColumnA],
                    [ColumnB],
                    [ColumnC],
                    ''SampleTitle''+
                      cast(row_number() over(partition by columna, columnb
                                              order by columnc) as varchar(10)) seq
                   from DataSource
                ) x
                pivot 
                (
                    max(columnc)
                    for seq in (' + @cols + ')
                ) p '
    
    execute sp_executesql @query;
    

    See SQL Fiddle with Demo. These give a result:

    | COLUMNA | COLUMNB | SAMPLETITLE1 | SAMPLETITLE2 | SAMPLETITLE3 |
    |---------|---------|--------------|--------------|--------------|
    |    5060 |    1006 |       100118 |       100119 |       100120 |
    |    5060 |    1007 |       100121 |       100122 |       (null) |
    |    5060 |    1012 |       100123 |       (null) |       (null) |
    
    0 讨论(0)
提交回复
热议问题