SQL Server复杂数据透视表查询

时间:2016-12-14 20:40:38

标签: sql-server

我的数据采用以下格式:

Customer_ID Order_ID Product_Sub-Category Product Name
=========== ======== ==================== ============
A00001      20001A   Vegetables           Onions
A00001      20001A   Vegetables           Garlic
A00002      20001B   Fruits               Apples
A00002      20001B   Fruits               Oranges
A00002      20001B   Vegetables           Spinach
A00003      20001C   Dairy                Milk
A00003      20001C   Dairy                Cheese
A00004      20001D   Meats                Lamb Chops
A00004      20001D   Meats                T-bone Steak
A00004      20001D   Dairy                Yoghurt
A00004      20001D   Fruits               Grapes
A00004      20001D   Vegetables           Garlic

我需要将其转换为以下格式。

Customer_ID Order_ID Vegetables     Fruits          Dairy        Meats
=========== ======== ==========     ======          =====        =====
A00001      20001A   Onions, Garlic
A00002      20001B   Spinach        Apples, Oranges 
A00003      20001C                                  Milk, Cheese    
A00004      20001D   Garlic         Grapes          Yoghurt      Lamb Chops, T-bone Steak

如果可以在SQL查询中完成此操作,请告诉我

1 个答案:

答案 0 :(得分:0)

假设您想要或需要动态。

  

编辑 - 为性能添加了选择区别

Declare @SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName([Product_Sub-Category]) From YourTable Order by 1 For XML Path('')),1,1,'') 
Select  @SQL = '
 Select [Customer_ID],[Order_ID],' + @SQL + '
  From (
        Select A.Customer_ID
              ,A.Order_ID
              ,A.[Product_Sub-Category]
              ,C.String
         From  (Select Distinct Customer_ID,Order_ID,[Product_Sub-Category] From YourTable) A 
         Cross Apply (
                        Select String=Stuff((Select Distinct '','' +[Product Name] 
                                             From   YourTable 
                                             Where Customer_ID = A.Customer_ID 
                                               and Order_ID = A.Order_ID 
                                               and [Product_Sub-Category] = A.[Product_Sub-Category] 
                                             For XML Path ('''')),1,1,'''') 
                     ) C
       ) A
 Pivot (Max(String) For [Product_Sub-Category] in (' + @SQL + ') ) p'
Exec(@SQL);

返回

enter image description here

如果有帮助,如果您不需要动态,则生成的SQL如下

 Select [Customer_ID],[Order_ID],[Dairy],[Fruits],[Meats],[Vegetables]
  From (
        Select A.Customer_ID
              ,A.Order_ID
              ,A.[Product_Sub-Category]
              ,C.String
         From  (Select Distinct Customer_ID,Order_ID,[Product_Sub-Category] From YourTable) A 
         Cross Apply (
                        Select String=Stuff((Select Distinct ',' +[Product Name] 
                                              From  YourTable 
                                              Where Customer_ID=A.Customer_ID 
                                                and Order_ID=A.Order_ID 
                                                and [Product_Sub-Category]=A.[Product_Sub-Category] 
                                              For XML Path ('')),1,1,'') 
                     ) C
       ) A
 Pivot (Max(String) For [Product_Sub-Category] in ([Dairy],[Fruits],[Meats],[Vegetables]) ) p