Sybase數據庫 行列轉換

數據表的內容如下:(這是一個人的一年的一個數據)
[img]http://dl.iteye.com/upload/attachment/487533/e7df0aac-85dd-36eb-97c2-d285249797df.jpg[/img]
現在需要把Month和OP字段行列轉換,這兩個字段都是整型的。

轉換的sql語句如下:(我測試使用的數據是Sybase)

select SaleOrDisID,Year,
max(case when Month=1 then OP else 0 end) as 1月,
max(case when Month=2 then OP else 0 end) as 2月,
max(case when Month=3 then OP else 0 end) as 3月,
max(case when Month=4 then OP else 0 end) as 4月,
max(case when Month=5 then OP else 0 end) as 5月,
max(case when Month=6 then OP else 0 end) as 6月,
max(case when Month=7 then OP else 0 end) as 7月,
max(case when Month=8 then OP else 0 end) as 8月,
max(case when Month=9 then OP else 0 end) as 9月,
max(case when Month=10 then OP else 0 end) as 10月,
max(case when Month=11 then OP else 0 end) as 11月,
max(case when Month=12 then OP else 0 end) as 12月
from tableName
group by SaleOrDisID,Year


執行的結果是:
[table]
|SaleOrDisID |Year |1月 |2月 |3月 |4月 |5月 |6月 |7月 |8月 |9月 |10月 |11月 |12月|
|1 |2011 |10 |10 |10 |10 |30 |25 |30 |30 |30 |30 |30 |30|
[/table]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章