Search code examples
pivotunpivot

static column name in pivot table


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


select @cols = '[1015],[1060],[1261],[1373]'

print @cols



set @query = 'SELECT header, ' + @cols + ' 
              from 
             (
                select product_id, header, value
                from
                (
                  select 
                        cast(product_id  as varchar(100))product_id , 
                        cast(product_name  as varchar(100)) product_name,
                        cast(product_price as varchar(100)) product_price, 
                        cast(product_weight as  varchar(100))product_weight,
                        cast((select TOP 1  image_name from tblProductImage where tblProductImage.product_id=tblProduct.product_id) as  varchar(100)) ProductImage      
                  from tblProduct 

                ) p
                unpivot
                (
                  value
                  for header in (product_name, product_price, product_weight,ProductImage)
                ) unp
            ) x
            pivot 
            (
                max(value)
                for product_id in (' + @cols + ')
            ) p '

execute(@query)

I am using above query and its generating following output

enter image description here

Now my problem is i want that column name should be static .. as there currently there max to max 5 columns out of them first column will be header which is OK

but for rest of column's i want also there names should be like Prodcut1,Product2,Product3, and Product4 . In case if there only three products then its also shows five columns but for last column all attributes value should be null


Solution

  • If I am understanding your question correctly then while you are passing in specific product_ids, you want the column names to be Product1, Product2, etc.

    If that is correct, then you can create the static column names by applying row_number().

    Your code will be adjusted to the following:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    DECLARE @prods AS NVARCHAR(MAX)
    
    select @prods = '141,142,143,144'
    
    set @query = 'SELECT header, Product1, Product2, Product3, Product4
                  from 
                  (
                    select header, value,
                      ''Product''+cast(row_number() over(partition by header 
                                                       order by header) as varchar(10)) prods
                    from
                    (
                      select 
                        cast(product_id as varchar(10)) product_id,
                        product_name, 
                        cast(product_price as varchar(10)) product_price, 
                        product_weight
                      from tblProduct
                      where product_id in ('+@prods+') 
                    ) p
                    unpivot
                    (
                      value
                      for header in (product_name, product_price, product_weight, product_id)
                    ) unp
                ) x
                pivot 
                (
                    max(value)
                    for prods in (Product1, Product2, Product3, Product4)
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo. This gives the result:

    |         HEADER | PRODUCT1 | PRODUCT2 | PRODUCT3 | PRODUCT4 |
    --------------------------------------------------------------
    |     product_id |      141 |      142 |      143 |      144 |
    |   product_name |     A141 |     A144 |     A143 |     A142 |
    |  product_price |      300 |     4000 |     5000 |      200 |
    | product_weight |    200gm |    100gm |    400gm |    300gm |