我需要在SQL Server 2005中使用查询将多行的结果合并为一行
我所拥有的数据与这样的服装尺寸有关:
# Item No. Garment SKU In Stock
1 CUR211NA-L CUR211NA 0.00
2 CUR211NA-LB CUR211NA 10.00
3 CUR211NA-M CUR211NA 0.00
4 CUR211NA-MB CUR211NA 3.00
5 CUR211NA-S CUR211NA 0.00
6 CUR211NA-SB CUR211NA -9.00
7 CUR211NA-XL CUR211NA 0.00
8 CUR211NA-XXL CUR211NA 0.00
9 CUR211NA-YTH CUR211NA 7.00
我需要显示一个SKU代码行,其大小在列中,如下所示:
SB MB LB YTH S M L XL XXL
CUR211NA -9 3 10 7 0 0 0 0 0
到目前为止,我正确地显示了数量但是将每个项目放在单独的行上
SELECT distinct T0.[U_GarmentSKU], T0.[U_Garment_Title],
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('47900') and t0.itemcode=t1.itemcode) 'SB',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('39600') and t0.itemcode=t1.itemcode) as 'MB',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('38500') and t0.itemcode=t1.itemcode) as 'LB',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('50100') and t0.itemcode=t1.itemcode) as 'YTH',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('47700') and t0.itemcode=t1.itemcode) as 'S',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('39400') and t0.itemcode=t1.itemcode) as 'M',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('38300') and t0.itemcode=t1.itemcode) as 'L',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('49200','48700') and t0.itemcode=t1.itemcode) as 'XL',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('49700','49300') and t0.itemcode=t1.itemcode) as 'XXL',
(select T0.[OnHand] from oitm t1 where T0.[U_GarmentSize] IN ('49800') and t0.itemcode=t1.itemcode) as 'XXXL'
FROM OITM T0 WHERE T0.[U_GarmentSKU] like 'SUR%' and T0.[U_StkStat] = 'G'
答案 0 :(得分:1)
不要使用子查询。使用pivot
(好吧,不在SQL Server 2005中)或条件聚合:
SELECT T0.[U_GarmentSKU], T0.[U_Garment_Title],
SUM(CASE WHEN T0.[U_GarmentSize] IN ('47900') THEN T0.[OnHand] ELSE 0
END) as SB,
. . .
FROM OITM T0
WHERE T0.[U_GarmentSKU] like 'SUR%' and T0.[U_StkStat] = 'G'
GROUP BY T0.[U_GarmentSKU], T0.[U_Garment_Title];
答案 1 :(得分:0)
与Gordon Linoff的回答相同的条件聚合,只是扩展了问题中的所有列:
select
t.[U_GarmentSKU]
, t.[U_Garment_Title]
, [SB] = sum(case when t.[U_GarmentSize] in ('47900') then t.[OnHand] else 0 end)
, [MB] = sum(case when t.[U_GarmentSize] in ('39600') then t.[OnHand] else 0 end)
, [LB] = sum(case when t.[U_GarmentSize] in ('38500') then t.[OnHand] else 0 end)
, [YTH] = sum(case when t.[U_GarmentSize] in ('50100') then t.[OnHand] else 0 end)
, [S] = sum(case when t.[U_GarmentSize] in ('47700') then t.[OnHand] else 0 end)
, [M] = sum(case when t.[U_GarmentSize] in ('39400') then t.[OnHand] else 0 end)
, [L] = sum(case when t.[U_GarmentSize] in ('38300') then t.[OnHand] else 0 end)
, [XL] = sum(case when t.[U_GarmentSize] in ('49200','48700') then t.[OnHand] else 0 end)
, [XXL] = sum(case when t.[U_GarmentSize] in ('49700','49300') then t.[OnHand] else 0 end)
, [XXXL] = sum(case when t.[U_GarmentSize] in ('49800') then t.[OnHand] else 0 end)
from oitm t
where t.[U_GarmentSKU] like 'SUR%'
and t.[U_StkStat] = 'G'
group by t.[U_GarmentSKU], t.[U_Garment_Title]