如何实现sqlresult集的传输?

时间:2014-06-03 11:37:35

标签: sql visual-studio-2010

    January February March April
  500     200      300   400

下面是我的代码:

select 
 sum (case when [Month] = 1 then forecastdemand else 0.0  end ) January,
 sum(case when [Month] = 2 then forecastdemand  else 0.0 end) February,
  sum(case when [Month] = 3 then forecastdemand  else 0.0 end) March ,
 sum(case when [Month] = 4 then forecastdemand  else 0.0 end) April ,
 sum(case when [Month] = 5 then forecastdemand  else 0.0 end) May ,
 sum(case when [Month] = 6 then forecastdemand  else 0.0 end) June ,
  sum(case when [Month] = 7 then forecastdemand  else 0.0 end) July ,
 sum(case when [Month] = 8 then forecastdemand  else 0.0 end) August ,
  sum(case when [Month] = 9 then forecastdemand  else 0.0 end) September ,
 sum(case when [Month] = 10 then forecastdemand  else 0.0 end) October ,
 sum(case when [Month] = 11 then forecastdemand  else 0.0 end) November ,
 sum(case when [Month] = 12 then forecastdemand  else 0.0 end) December 
from forecastreorder as result
where Productid = 2

我怎样才能进入

  Month     Result
January   500
February  200
March     300
April     400

我知道我可以使用pivot unpivot但是我怎么能应用它? 谢谢你的高级:)

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

select (case when [Month] = 1 then 'January'
             when [Month] = 2 then 'February'
             when [Month] = 3 then 'March'
             when [Month] = 4 then 'April'
        end) as Mon, sum(forecastdemand) as Result           
from forecastreorder fr
where Productid = 2 and
      [Month] in (1, 2, 3, 4)
group by [Month]
order by [Month];