Multi Column Dynamic Pivot Table

I am trying to get a clean Pivot Table for multiple columns.

Create Input Table

create table #temp (
  ORDER_ID INT NOT NULL,
  TEST_PLAN INT NOT NULL,
  COLLECTION_TYPE INT NOT NULL,
  TEST_GRP INT NOT NULL,
  TEST INT NOT NULL
)

INSERT INTO #temp (ORDER_ID,TEST_PLAN,COLLECTION_TYPE,TEST_GRP,TEST) VALUES (1,1,2,1360942998,1360943100)
INSERT INTO #temp (ORDER_ID,TEST_PLAN,COLLECTION_TYPE,TEST_GRP,TEST) VALUES (2,1,2,1360943006,1360943079)
INSERT INTO #temp (ORDER_ID,TEST_PLAN,COLLECTION_TYPE,TEST_GRP,TEST) VALUES (1,2,2,1360942845,1360943173)
INSERT INTO #temp (ORDER_ID,TEST_PLAN,COLLECTION_TYPE,TEST_GRP,TEST) VALUES (2,2,2,1360942845,1360943134)
INSERT INTO #temp (ORDER_ID,TEST_PLAN,COLLECTION_TYPE,TEST_GRP,TEST) VALUES (3,2,2,1360942845,1360943189)
INSERT INTO #temp (ORDER_ID,TEST_PLAN,COLLECTION_TYPE,TEST_GRP,TEST) VALUES (1,3,2,1360942998,1360943100)

Result...

ORDER_ID PLAN COLLECTION_TYPE TEST_GRP   TEST        
-------- ---- --------------- ---------- ----------
1        1    2               1360942998 1360943100
2        1    2               1360943006 1360943079
1        2    1               1360942845 1360943173
2        2    1               1360942845 1360943134
3        2    1               1360942845 1360943189
1        3    2               1360942998 1360943100

I would like the following, where the ORDER_ID is appended to the COLLECTION_TYPE,TEST_GRP, and TEST columns

PLAN COLLECTION_TYPE_1 TEST_GRP_1 TEST_1     COLLECTION_TYPE_2 TEST_GRP_2 TEST_2     COLLECTION_TYPE_3 TEST_GRP_3 TEST_3
---- ----------------- ---------- ---------- ----------------- ---------- ---------- ----------------- ---------- ----------
1    2                 1360942998 1360943100 2                 1360943006 1360943079  NULL              NULL       NULL
2    1                 1360942845 1360943173 1                 1360942845 1360943134 1                 1360942845 1360943189
3    2                 1360942998 1360943100 NULL              NULL       NULL       NULL              NULL       NULL 

I have this and it works, but was looking for something a little cleaner (e.g. Few Nulls).

DECLARE  @SQL  NVARCHAR(MAX),
         @Cols NVARCHAR(MAX)

SELECT @cols = STUFF((select ',
MAX(CASE WHEN [TEST_PLAN]=' + CONVERT(VARCHAR,[TEST_PLAN]) + ' AND [ORDER_ID] = ' + CONVERT(VARCHAR,[ORDER_ID]) +
' THEN [TEST_GRP] ELSE NULL END) AS [TEST_GRP_' + CONVERT(VARCHAR,[ORDER_ID]) + '],
MAX(CASE WHEN [TEST_PLAN]=' + CONVERT(VARCHAR,[TEST_PLAN]) + ' AND [ORDER_ID] = ' + CONVERT(VARCHAR,[ORDER_ID]) +
' THEN [TEST_GRP] ELSE NULL END) AS [TEST_' + CONVERT(VARCHAR,[ORDER_ID]) + '],
MAX(CASE WHEN [TEST_PLAN]=' + CONVERT(VARCHAR,[TEST_PLAN]) + ' AND [ORDER_ID] = ' + CONVERT(VARCHAR,[ORDER_ID]) +
' THEN [COLLECTION_TYPE] ELSE NULL END) AS [COLLECTION_TYPE_' + CONVERT(VARCHAR,[ORDER_ID]) + ']'
FROM #temp
ORDER BY [TEST_PLAN],[ORDER_ID] FOR XML PATH(''),type).value('.','varchar(max)'),1,2,'')

SET @SQL = 'SELECT TEST_PLAN,' + @Cols + ' FROM #Temp GROUP BY TEST_PLAN'

EXECUTE( @SQL)

There is the output...

TEST_PLAN   TEST_GRP_1  TEST_1      COLLECTION_TYPE_1 TEST_GRP_2  TEST_2      COLLECTION_TYPE_2 TEST_GRP_1  TEST_1      COLLECTION_TYPE_1 TEST_GRP_2  TEST_2      COLLECTION_TYPE_2 TEST_GRP_3  TEST_3      COLLECTION_TYPE_3 TEST_GRP_4  TEST_4      COLLECTION_TYPE_4 TEST_GRP_5  TEST_5      COLLECTION_TYPE_5 TEST_GRP_6  TEST_6      COLLECTION_TYPE_6 TEST_GRP_7  TEST_7      COLLECTION_TYPE_7 TEST_GRP_8  TEST_8      COLLECTION_TYPE_8 TEST_GRP_9  TEST_9      COLLECTION_TYPE_9 TEST_GRP_10 TEST_10     COLLECTION_TYPE_10 TEST_GRP_1  TEST_1      COLLECTION_TYPE_1
----------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ----------------- ----------- ----------- ------------------ ----------- ----------- -----------------
1           1360942998  1360942998  2                 1360943006  1360943006  2                 NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL               NULL        NULL        NULL
2           NULL        NULL        NULL              NULL        NULL        NULL              1360942845  1360942845  2                 1360942845  1360942845  2                 1360942845  1360942845  2                 1360942845  1360942845  2                 1360942845  1360942845  2                 1360942845  1360942845  2                 1360942845  1360942845  2                 1360942845  1360942845  2                 1360942845  1360942845  2                 1360942845  1360942845  2                  NULL        NULL        NULL
3           NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL              NULL        NULL        NULL               1360942998  1360942998  2

I have searched for a solution with the closest being the above sql.

Thanks jlimited


ANSWERS:


In order to get the result that you want, I would have to use both the UNPIVOT and the PIVOT functions. The UNPIVOT will take your values from columns and convert it to rows and the PIVOT takes the rows and converts it back to columns.

Sometimes it is easier to use a static or hard-coded version of the query first and then convert to dynamic SQL. The static version will be:

select [Plan],
  Isnull(COLLECTION_TYPE_1, '') COLLECTION_TYPE_1, 
  Isnull(TEST_GRP_1, '') TEST_GRP_1, 
  Isnull(TEST_1, '') TEST_1,
  Isnull(COLLECTION_TYPE_2, '') COLLECTION_TYPE_2, 
  Isnull(TEST_GRP_2, '') TEST_GRP_2, 
  Isnull(TEST_2, '') TEST_2,
  Isnull(COLLECTION_TYPE_3, '') COLLECTION_TYPE_3, 
  Isnull(TEST_GRP_3, '') TEST_GRP_3, 
  Isnull(TEST_3, '') TEST_3
from
(
  select [PLAN], col + '_'+ cast(ORDER_ID as varchar(50)) col, value
  from
  (
    select ORDER_ID,[PLAN],COLLECTION_TYPE,TEST_GRP,TEST
    from temp
  ) s
  unpivot
  (
    value
    for col in (COLLECTION_TYPE,TEST_GRP,TEST)
  ) unpiv
) src
pivot
(
  max(value)
  for col in (COLLECTION_TYPE_1, TEST_GRP_1, TEST_1,
              COLLECTION_TYPE_2, TEST_GRP_2, TEST_2,
              COLLECTION_TYPE_3, TEST_GRP_3, TEST_3)
) piv

See SQL Fiddle with Demo.

Once you have the static version, then you can easily convert this to dynamic SQL. When generating the dynamic SQL, you can create a list of the columns that replaces the null values with an empty string or another value which cleans up the nulls. The dynamic SQL code is:

DECLARE @cols AS NVARCHAR(MAX),
    @colsNames AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(col + '_'+ cast(ORDER_ID as varchar(50))) 
                    from temp t
                    cross apply 
                    (
                      select 'COLLECTION_TYPE' col, 1 SortOrder
                      union all
                      select 'TEST_GRP' col, 2 SortOrder
                      union all
                      select 'TEST' col, 3 SortOrder
                    ) c
                    group by col, ORDER_ID, sortorder
                    order by ORDER_ID, sortorder
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

select @colsNames = STUFF((SELECT ', IsNull(' + QUOTENAME(col + '_'+ cast(ORDER_ID as varchar(50)))+', '''') as '+QUOTENAME(col + '_'+ cast(ORDER_ID as varchar(50)))
                    from temp t
                    cross apply 
                    (
                      select 'COLLECTION_TYPE' col, 1 SortOrder
                      union all
                      select 'TEST_GRP' col, 2 SortOrder
                      union all
                      select 'TEST' col, 3 SortOrder
                    ) c
                    group by col, ORDER_ID, sortorder
                    order by ORDER_ID, sortorder
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT [PLAN],' + @colsNames + ' from 
             (
                select [PLAN], col + ''_''+ cast(ORDER_ID as varchar(50)) col, value
                from
                (
                  select ORDER_ID,[PLAN],COLLECTION_TYPE,TEST_GRP,TEST
                  from temp
                ) s
                unpivot
                (
                  value
                  for col in (COLLECTION_TYPE,TEST_GRP,TEST)
                ) unpiv
            ) src
            pivot 
            (
                max(value)
                for col in (' + @cols + ')
            ) p '

execute(@query)

See SQL Fiddle with Demo

Both give the result:

| PLAN | COLLECTION_TYPE_1 | TEST_GRP_1 |     TEST_1 | COLLECTION_TYPE_2 | TEST_GRP_2 |     TEST_2 | COLLECTION_TYPE_3 | TEST_GRP_3 |     TEST_3 |
--------------------------------------------------------------------------------------------------------------------------------------------------
|    1 |                 2 | 1360942998 | 1360943100 |                 2 | 1360943006 | 1360943079 |                 0 |          0 |          0 |
|    2 |                 1 | 1360942845 | 1360943173 |                 1 | 1360942845 | 1360943134 |                 1 | 1360942845 | 1360943189 |
|    3 |                 2 | 1360942998 | 1360943100 |                 0 |          0 |          0 |                 0 |          0 |          0 |


 MORE:


 ? Dynamic PIVOT with Numbered Column Names
 ? SQL Server dynamic PIVOT query - fields order
 ? T-SQL dynamic pivot
 ? T-SQL dynamic pivot
 ? T-SQL dynamic pivot
 ? Converting Row to Column dynamically in SQL
 ? I'm currently looking for a dynamic way to convert rows to columns in a specific way in SQL server
 ? Microsoft SQL Server: Adding New Column To Row Instead Of Repeated Values
 ? Joining tables and LEFT JOIN as new Columns in tsql
 ? SQL query which returns variable number of columns for each record combining multiple rows