Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 转换SQL Server查询结果_Sql Server_Tsql_Pivot - Fatal编程技术网

Sql server 转换SQL Server查询结果

Sql server 转换SQL Server查询结果,sql-server,tsql,pivot,Sql Server,Tsql,Pivot,我有一张桌子,上面有: 销售量 销售总数 价格区间销售数据 这是我的桌子: Year || Item_Category || Dollar_Volume || No_of_Sales || $0-$99 || $100-$199 || $200-$299 2018 || Produce || 2359 || 23 || 13 || 4 || 6 2018 || Clothing || 4325

我有一张桌子,上面有:

销售量 销售总数 价格区间销售数据 这是我的桌子:

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,15级,状态1,第105行 “,”附近的语法不正确


我看到您使用的表已经是PIVOT了。 我建议你把它拆开,然后把它转到另一个。
它应该可以工作

您可以使用pivot和cross 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