日期:2014-05-16  浏览次数:20900 次

新手求教oracle行转列问题
原表数据:
ID_NO_SZ NAME_SZ BAN DIR DAYS
2000139 孔令波 D01+E01 間接 1
2000139 孔令波 D02+E02 間接 26
2000225 楊長永 D02+E02 間接 24
2000245 潘亞平 D01+E01 直接 24
想转换成如下形式的求高手指点:
ID_NO_SZ NAME_SZ D01+E01 D02+E02 DIR
2000139 孔令波 1 26 間接
2000225 楊長永 0 24 間接
2000245 潘亞平 24 0 直接
(D01+E01 D02+E02 下面的数字为原表的days数值 )

------解决方案--------------------
--这两个都行.

SQL code
select ID_NO_SZ ,NAME_SZ,
  max(case BAN when 'D01+E01' then DAYS else 0 end) "D01+E01",
  max(case BAN when 'D02+E02' then DAYS else 0 end) "D02+E02",
  max(DIR) DIR
from tb
group by ID_NO_SZ ,NAME_SZ

select ID_NO_SZ ,NAME_SZ,
  max(decode (BAN ,'D01+E01' , DAYS , 0 )) "D01+E01",
  max(decode (BAN ,'D02+E02' , DAYS , 0 )) "D02+E02",
  max(DIR) DIR
from tb
group by ID_NO_SZ ,NAME_SZ

------解决方案--------------------
SQL code
select ID_NO_SZ ,NAME_SZ,
  max(case BAN when 'D01+E01' then DAYS else 0 end) "D01+E01",
  max(case BAN when 'D02+E02' then DAYS else 0 end) "D02+E02",
  max(DIR) DIR
from tb
group by ID_NO_SZ ,NAME_SZ

------解决方案--------------------
SQL code
select ID_NO_SZ ,NAME_SZ,
  max(decode (BAN ,'D01+E01' , DAYS , 0 )) "TA夜班出勤天数1",
  max(decode (BAN ,'D02+E02' , DAYS , 0 )) "TA夜班出勤天数2",
  max(DIR) DIR
from tb
group by ID_NO_SZ ,NAME_SZ