有数据如下图所示得到格式化输出
ItemCode SalesPrice PricingLevel
ITEM-000001 451.000000 Barons
ITEM-000001 432.000000 Guild
有没有一种方法可以得到以下的输出:
ItemCode Barons Guild
ITEM-000001 451 432
有数据如下图所示得到格式化输出
ItemCode SalesPrice PricingLevel
ITEM-000001 451.000000 Barons
ITEM-000001 432.000000 Guild
有没有一种方法可以得到以下的输出:
ItemCode Barons Guild
ITEM-000001 451 432
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
您好shahkalpesh,我使用您的查询,因为我觉得它比以上更快,因为在您的查询中只有一个选择&在以上3选择,所以我觉得使用你的解决方案更好。 非常感谢。 – 2010-02-01 06:55:10
刚刚尝试这一点(注:我不知道你的表名,我把它叫做 “项目”):
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
嗨Tufo它工作正常,但是它工作正常,即性能的观点,如果我有巨大的数据? – 2010-01-29 15:29:14
如果你只有那些列,所以它会正常工作,它会为整个表加1选择,每个项目2,没有问题。但是如果你有更多的专栏,表现可能是一个问题。您有多少个PricingLevel寄存器? – Tufo 2010-01-29 15:34:30
在该表中,我有15列,但pricinglevel固定 – 2010-01-29 15:39:55
您有多少种不同PricingLevel?我可以看到PricingLevel(Barons,Guild)的2个值。该列表中可能有更多项目吗? – shahkalpesh 2010-01-29 15:24:39
只有2,它工作正常。 – 2010-01-29 16:17:11