注册 登录
编程论坛 Oracle论坛

sql查询问题

ggvboy 发布于 2008-11-26 16:01, 1424 次点击
A表6个字段,分别为:
    emp    dept    year    month    x1    x2   
B表6个字段,分别为:
    emp    dept    year    month    x3    x4
无主键,无非空约束,人员数为根据某一个部门和月份查询出的emp的个数,假如查询的年份为2007年,其中4到12月为2007年的月份,1到3月为2008年的月份。

查出如下结构

        4月    5月    6月    7月    8月    9月    10月    11月    12月    1月    2月    3月

人员数
(x1+x2)
(x3+x4)   

解决不了没工作,要效率贴,希望各位巨侠帮忙!
3 回复
#2
gdy03492008-11-26 17:52
方法笨了点,但能解决问题
select a.*,b.* from(
select a.emp,decode(a.month,'04',x1+x2),decode(a.month,'05',x1+x2),decode(a.month,'06',x1+x2),decode(a.month,'07',x1+x2),decode(a.month,'08',x1+x2),decode(a.month,'09',x1+x2)
,decode(a.month,'10',x1+x2),decode(a.month,'11',x1+x2),decode(a.month,'12',x1+x2) from a where a.year=2007) a(
select a.emp,decode(a.month,'01',x1+x2),decode(a.month,'02',x1+x2),decode(a.month,'03',x1+x2) from a where a.year=2008) b where a.emp=b.emp
#3
ggvboy2008-12-09 16:48
的确是笨,但不能解决问题,我只是把SQL要查的几个字段说出来,开始我差不多就是这么写的,写了1千多行,查处数据耗时4秒多,在CSDN上也没有好的结果,我就是想按(4,5,6,7,8,9,10,11,12,1,2,3)这个排序,能简单点,头疼。
1