获取格式化的输出

时间:2010-01-29 15:08:26

标签: sql-server-2005

拥有如下所示的数据

ItemCode    SalesPrice  PricingLevel
ITEM-000001 451.000000  Barons
ITEM-000001 432.000000  Guild

是否有办法获得以下输出:

ItemCode      Barons     Guild
ITEM-000001   451        432

2 个答案:

答案 0 :(得分:1)

SELECT ItemCode, 
Sum(Case when PricingLevel = 'Barons' Then SalesPrice else 0 end) as Barons,
Sum(Case when PricingLevel = 'Guild' Then SalesPrice else 0 end) as Guild
FROM myTable
GROUP BY ItemCode

答案 1 :(得分:0)

试试这个(注意:因为我不知道你的表名,我称之为“项目”):

SELECT DISTINCT I1.ItemCode, 
       (SELECT SalesPrice FROM Items I2 WHERE I2.ItemCode = I1.ItemCode AND I2.PricingLevel = 'Barons') Barons,
       (SELECT SalesPrice FROM Items I3 WHERE I3.ItemCode = I1.ItemCode AND I3.PricingLevel = 'Guild') Guild
FROM Items I1

如果不显示小数零,请使用以下命令:

SELECT DISTINCT I1.ItemCode, 
       (SELECT CAST(SalesPrice AS DECIMAL(10,0)) FROM Items I2 WHERE I2.ItemCode = I1.ItemCode AND I2.PricingLevel = 'Barons') Barons,
       (SELECT CAST(SalesPrice AS DECIMAL(10,0)) FROM Items I3 WHERE I3.ItemCode = I1.ItemCode AND I3.PricingLevel = 'Guild') Guild
FROM Items I1