我在做一个交叉报表,现在已oracle的scott的emp表为基础。在EMP表的原始字段上,新建一个字段state,字段值为1和0,先要做成如下:主要用到字段deptno,state,已经sum(sal).
10部门       20部门         30部门                        --deptno
0状态工资     0状态工资 1状态工资 0状态工资 1状态工资     --state8750      3000     4875              4350         3800         --sum(sal)
在线等待,多谢!
 

解决方案 »

  1.   

    最好能直接帮我把SQL语句写出来,多谢各位大大。
      

  2.   

    select deptno,state,sum(sal) from emp group by deptno,state
      

  3.   


    --state字段增加过了吗?
    alter table emp add(state number(1));
      

  4.   

     
    SQL> select * from emp;
     
    EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO      STATE
    ----- ---------- --------- ----- ----------- --------- --------- ------ ----------
        1 SMITH      CLERK      7902 1980-12-17     800.00               20          0
        2 ALLEN      SALESMAN   7698 1981-2-20     1600.00    300.00     30          1
        3 WARD       SALESMAN   7698 1981-2-22     1250.00    500.00     30          1
        4 JONES      MANAGER    7839 1981-4-2      2975.00               20          0
        5 MARTIN     SALESMAN   7698 1981-9-28     1250.00   1400.00     30          1
        6 BLAKE      MANAGER    7839 1981-5-1      2850.00               30          0
        7 CLARK      MANAGER    7839 1981-6-9      2450.00               10          1
        8 SCOTT      ANALYST    7566 1987-4-19     3000.00               20          0
        9 KING       PRESIDENT       1981-11-17    5000.00               10          1
       10 TURNER     SALESMAN   7698 1981-9-8      1500.00      0.00     30          1
       11 ADAMS      CLERK      7788 1987-5-23     1100.00               20          0
       12 JAMES      CLERK      7698 1981-12-3      950.00               30          1
       13 FORD       ANALYST    7566 1981-12-3     3000.00               20          1
       14 MILLER     CLERK      7782 1982-1-23     1300.00               10          0
     
    14 rows selected
     
    SQL> 
    SQL> select sum(case when state=0 and deptno=10  then sal else 0 end) "10部门0状态",
      2          sum(case when state=1 and deptno=10  then sal else 0 end) "10部门1状态",
      3          sum(case when state=0 and deptno=20  then sal else 0 end) "20部门0状态",
      4          sum(case when state=1 and deptno=20  then sal else 0 end) "20部门1状态",
      5          sum(case when state=0 and deptno=30  then sal else 0 end) "30部门0状态",
      6          sum(case when state=1 and deptno=30  then sal else 0 end) "30部门1状态" from emp;
     
        10部门0状态     10部门1状态     20部门0状态     20部门1状态     30部门0状态     30部门1状态
    ----------- ----------- ----------- ----------- ----------- -----------
           1300        7450        7875        3000        2850        6550
     
      

  5.   

    不好意思我要的是交叉报表

    linzhangqs写的大概是我想要的,不过不是这样
      

  6.   

    我想要的是,
       10部门
    0状态      1状态
    sum(sal)  sum(sal)
      

  7.   

    那你要得到四行吗?第一行,部门,第二行,状态第三行
    sum(sal)  sum(sal)
    第四行结果吗
      

  8.   

    select t.deptno, t.state, sum(t.sal) from emp t group by CUBE(t.state,t.deptno)  having t.deptno is not null and t.state is not null;
      

  9.   

    给你拼起来了....SQL> select '0状态' as "10部门",'1状态' as " ", '0状态' as "20部门",'1状态' as " ",
      2   '0状态' as "30部门",'1状态' as " " from dual
      3    union
      4  select to_char(sum(case when state=0 and deptno=10  then sal else 0 end)) ,
      5          to_char(sum(case when state=1 and deptno=10  then sal else 0 end)) ,
      6          to_char(sum(case when state=0 and deptno=20  then sal else 0 end)),
      7          to_char(sum(case when state=1 and deptno=20  then sal else 0 end) ),
      8          to_char(sum(case when state=0 and deptno=30  then sal else 0 end)) ,
      9          to_char(sum(case when state=1 and deptno=30  then sal else 0 end))  from emp
     10  ;
     
    10部门                                                                            20部门                                                                            30部门                                   
    ---------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- ----------------------------------------
    0状态                                    1状态                                    0状态                                    1状态                                    0状态                                    1状态
    1300                                     7450                                     7875                                     3000                                     2850                                     6550
     
    SQL> 
      

  10.   

    再sql里还得设值行间距。直接用plsql得到如下还算整齐10部门       20部门        30部门   
    0状态 1状态 0状态 1状态 0状态 1状态 
    1300  7450  7875  3000  2850 6550 
      

  11.   

    固定报表,要改的时候再改,就行,14楼的哥们貌似完成了。感谢之!若还有不同见解见解请发发SQL看看,共同讨论,多谢各位大大。此贴我下星期一结贴,到时候还回加分!
      

  12.   

    将表头两行做好,比如放在word文件中,把如下的运行结果copy后,粘贴在第三行即可:select sum(decode(state,0,sal,0)),sum(decode(state,1,sal,0)) from emp group by deptno;
      

  13.   

    也不一定要用plsql实现,很多报表工具都可以实现的,这些写死只能让一个报表使用,最好还是做成通用的,由不同格式的来展现