【发布时间】:2010-01-29 20:13:13
【问题描述】:
我有一个生产表,每个月有 12 列。我需要创建一个 SP,在其中传入 productID、Customer 和 Month 参数并检索该月的总和。
目前我的逻辑是
if month = 1 then
select sum(JAN) from table where productID = @id and customer = @cust
if month = 2 then
select SUM(FEB) from table where productID = @id and customer = @cust
....
查询有点复杂,但这是它的核心。有没有办法绕过这些“IF”语句?
编辑 - 这是一个 SQL Server 2000 数据库,但将迁移到 SQL Server 2005,当我迁移到 SQL Server 2005 时,PIVOT 和 UNPIVOT 将派上用场。
【问题讨论】:
-
答案都非常好。谢谢
标签: sql sql-server sql-server-2000