in

SDT Community Server

SDT Forums, Blogs, Photos server.

This Blog

Syndication

Coolboy

关于oracle查询日期横向显示的问题

表结构:
人员编码 月份 工时 天数
  001    1    12  5
  001    2    20  6
  001    3    18  8
  002    1    12  5
  002    2    15  6
  002    3    18  7

如何通过sql语句查询出如下显示结果
人员编码  内容    1月  2月  3月 ......12月
  001    工时    12  20  18
  001    天数    5    6    8
  002    工时    12  15  18
  002    天数    5    6    7

 

select 人员编码,
decode(rn,
1,'工时',2,'天数') 内容,
sum(decode(月份,1,decode(rn,1,工时,2,天数),0)) 1月,
sum(decode(月份,2,decode(rn,1,工时,2,天数),0)) 2月,
sum(decode(月份,3,decode(rn,1,工时,2,天数),0)) 3月,
sum(decode(月份,4,decode(rn,1,工时,2,天数),0)) 4月,
sum(decode(月份,5,decode(rn,1,工时,2,天数),0)) 5月,
sum(decode(月份,6,decode(rn,1,工时,2,天数),0)) 6月,
sum(decode(月份,7,decode(rn,1,工时,2,天数),0)) 7月,
sum(decode(月份,8,decode(rn,1,工时,2,天数),0)) 8月,
sum(decode(月份,9,decode(rn,1,工时,2,天数),0)) 9月,
sum(decode(月份,10,decode(rn,1,工时,2,天数),0)) 10月,
sum(decode(月份,11,decode(rn,1,工时,2,天数),0)) 11月,
sum(decode(月份,12,decode(rn,1,工时,2,天数),0)) 12月
from mytab a,(select rownum as rn from dual connect by rownum<=2) b
group by 人员编码,rn
order by 人员编码,rn;

Comments

No Comments
Copyright SDT, 2006-2009. All rights reserved.