sorry
用decode()+sign()来处理,比较繁琐,但可以解决

解决方案 »

  1.   

    sum(case when dt between '20000101' and '20001231' then num else 0 end) as '2000',
    -〉
    sum(decode(trunc(datecol),2000,num,0))
      

  2.   

    SQL*PLus> desc emp;
     名称                                      是否为空? 类型
     ----------------------------------------- -------- -----------------------
     EMPNO                                              NUMBER(4)
     ENAME                                              VARCHAR2(10)
     JOB                                                VARCHAR2(9)
     MGR                                                NUMBER(4)
     HIREDATE                                           DATE
     SAL                                                NUMBER(7,2)
     COMM                                               NUMBER(7,2)
     DEPTNO                                             NUMBER(2)SQL*PLus> select job, deptno, count(*)
      2       from emp
      3       group by job, deptno;JOB           DEPTNO   COUNT(*)
    --------- ---------- ----------
    CLERK             10          2
    CLERK             20          4
    CLERK             30          2
    ANALYST           20          4
    MANAGER           10          2
    MANAGER           20          2
    MANAGER           30          2
    SALESMAN          30          8
    PRESIDENT         10          2已选择9行。SQL*PLus> select job,
      2          max( decode( deptno, 10, cnt, null ) ) dept_10,
      3          max( decode( deptno, 20, cnt, null ) ) dept_20,
      4          max( decode( deptno, 30, cnt, null ) ) dept_30,
      5          max( decode( deptno, 40, cnt, null ) ) dept_40
      6        from ( select job, deptno, count(*) cnt
      7               from emp
      8               group by job, deptno )
      9      group by job
     10     /JOB          DEPT_10    DEPT_20    DEPT_30    DEPT_40
    --------- ---------- ---------- ---------- ----------
    ANALYST                       4
    CLERK              2          4          2
    MANAGER            2          2          2
    PRESIDENT          2
    SALESMAN                                 8-----------------------------------------------------------------------------------
    各位,我有如下一个工资表,如:
       姓名  工资项   工资
       张三  基本工资  1000
       张三  岗位工资  2000
       张三  效益工资   200
       李四  基本工资  1000
       李四  效益工资  1000
       .......我需要用一个SQL语句得到如下所示的结果:
       姓名  基本工资  岗位工资  效益工资 .....
       张三  1000      2000       200     .....
       李四  1000         0      1000     .....
       ....
    select 姓名, sum(decode(工资项,'基本工资',工资,0)) 基本工资, 
      sum(decode(工资项,'岗位工资',工资,0)) 岗位工资, ....
    from yourtable
    group by 姓名
      

  3.   

    看来这个问题用ORACLE解决是有些困难,不过还是感谢各位!
    bzszp(SongZip) 大侠的sign()是个思路,可以变相转化大于、小于的问题。
    sum(decode(trunc(datecol),2000,num,0)) ,只能匹配固定值,跟
    sum(decode(to_char(datecol,'yyyy'),'2000',num,0))类似另外从beckhambobo(beckham) 大侠的SQL中知道,原来ORACLE再子查询中可以不用出给表别名再次感谢!请以后多关照!!