static column name in pivot table

白昼怎懂夜的黑 提交于 2019-12-13 04:07:16

问题


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

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


回答1:


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 |


来源:https://stackoverflow.com/questions/15682709/static-column-name-in-pivot-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!