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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值