现在的位置: 首页 > 综合 > 正文

使用DECODE语句将记录变换字段 形成交叉表

2012年04月04日 ⁄ 综合 ⁄ 共 408字 ⁄ 字号 评论关闭

  现有一个商品销售表sale,表结构为:

  month    char(6)      --月份

  sell    number(10,2)   --月销售金额

  现有数据为:

  200001  1000

  200002  1100

  200003  1200

 

  想要转化为以下结构的数据:

  month1  number(10,2)   --1月销售金额

  month2  number(10,2)   --2月销售金额

  month3  number(10,2)   --3月销售金额

    
SQL语句为:

select  

  sum(decode(substrb(month,5,2),'01',sell,0)),

  sum(decode(substrb(month,5,2),'02',sell,0)),

  sum(decode(substrb(month,5,2),'03',sell,0))
from sale

抱歉!评论已关闭.