转置SQL Server查询结果

时间:2018-05-22 20:09:48

标签: sql-server tsql pivot

我有一张桌子:

  • Sale_Volume
  • 总销售额
  • 价格范围明智的销售数据

这是我的表:

Year || Item_Category || Dollar_Volume || No_of_Sales || $0-$99  || $100-$199 || $200-$299
2018 || Produce       ||    2359       ||     23      ||   13    ||    4      ||    6
2018 || Clothing      ||    4325       ||     118     ||   21    ||   70      ||   27
2018 || Fruits        ||    3756       ||     19      ||   15    ||    3      ||    1
2018 || Vegetables    ||    9124       ||     64      ||   19    ||    0      ||   45
2018 || Packed Food   ||    1174       ||     91      ||    7    ||   67      ||   17
2018 || Detergent     ||     568       ||     103     ||   99    ||    4      ||    0
2018 || Entertainment ||    6127       ||     925     ||   32    ||  500      ||  393

我需要调换它以使预期结果为:

Year ||               || Produce || Clothing || Fruits || Vegetables || Packed Food || Detergent || Entertainment
2018 || Dollar_Volume ||    2359 ||   4325   ||  3756  ||    9124    ||    1174     ||   568     ||   6127
2018 || No_of_Sales   ||      23 ||    118   ||    19  ||      64    ||      91     ||   103     ||    925
2018 || $0-$99        ||      13 ||     21   ||    15  ||      19    ||       7     ||    99     ||     32
2018 || $100-$199     ||       4 ||     70   ||     3  ||       0    ||      67     ||     4     ||    500
2018 || $200-$299     ||       6 ||     27   ||     1  ||      45    ||      17     ||     0     ||    393

我正在尝试使用以下PIVOT查询:

select [Year],[Produce],[Clothing],[Fruits],[Vegetables],[Packed Food],[Detergent],[Entertainment]
from 
(SELECT [Year],[Item_Category],[Total Sales],[Dollar Volume],[$0 to $99],[$100 to $199],[$200 to $299] 
FROM RAVI_PROJECTS.dbo.YTD_Sales_Data
) s
PIVOT 
(MAX([$0 to $99]),MAX([$100 to $199]),MAX([$2000 to $2999])) 
for [Item_Category] in ([Produce],[Clothing],[Fruits],[Vegetables],[Packed Food],[Detergent],[Entertainment])
) p;

但是,我收到“PIVOT(”:

)旁边的行的错误
  

Msg 102,Level 15,State 1,Line 105
  ','。

附近的语法不正确

2 个答案:

答案 0 :(得分:1)

您可以使用pivotcross apply

来获得所需的结果
declare @tmp table (Year int, Item_Category nvarchar(100), Dollar_Volume int,
                    No_of_Sales int, [$0-$99] int, [$100-$199] int, [$200-$299] int)
insert into @tmp values
    (2018 , 'Produce'      , 2359, 23 , 13,   4,   6),
    (2018 , 'Clothing'     , 4325, 118, 21,  70,  27),
    (2018 , 'Fruits'       , 3756, 19 , 15,   3,   1),
    (2018 , 'Vegetables'   , 9124, 64 , 19,   0,  45),
    (2018 , 'Packed Food'  , 1174, 91 ,  7,  67,  17),
    (2018 , 'Detergent'    ,  568, 103, 99,   4,   0),
    (2018 , 'Entertainment', 6127, 925, 32, 500, 393)

select piv.Year, piv.COL as [Column], piv.Produce, piv.Clothing, piv.Fruits,
       piv.Vegetables, piv.[Packed Food], piv.Detergent, piv.Entertainment
from 
( 
    select [Item_Category] , COL,VAL, [YEAR], ORD
    from @tmp 
    CROSS APPLY (VALUES 
        ('Dollar_Volume',[Dollar_Volume],1), 
        ('No_of_Sales'  ,[No_of_Sales]  ,2), 
        ('$0-$99'       ,[$0-$99]       ,3), 
        ('$100-$199'    ,[$100-$199]    ,4), 
        ('$200-$299'    ,[$200-$299]    ,5)
    )CS (COL,VAL,ORD) 
) src 
pivot ( max(val) for [Item_Category] in ([Produce], [Clothing], [Fruits], [Vegetables], 
        [Packed Food], [Detergent], [Entertainment]) ) piv 
order by piv.ORD 

结果:

enter image description here

答案 1 :(得分:0)

我看到你使用的桌子已经是PIVOT了。 我建议你取消它,然后将它转移到另一个。 它应该工作

相关问题