请用一个sql语句得出结果,从t1,t2中取出如t3所列格式数据。t1:月份mon 部门dep 业绩yj
-------------------------------
一月份      01      10
一月份      02      10
一月份      03      5
二月份      02      8
二月份      04      9
三月份      03      8t2:部门dep      部门名称dname
--------------------------------
      01      国内业务一部
      02      国内业务二部
      03      国内业务三部
      04      国际业务部t3 (result):部门dep 一月份      二月份      三月份
--------------------------------------
      01      10        null      null
      02      10         8        null
      03      null       5        8
      04      null      null      9
请高手进来帮忙,非常感谢!

解决方案 »

  1.   

    SQL> with tt as
      2  (select '一月份' 月份mon, '01' 部门dep, 10 业绩yj from dual
      3  union all
      4  select '一月份', '02', 10 from dual
      5  union all
      6  select '一月份', '03', 5 from dual
      7  union all
      8  select '二月份', '02', 8 from dual
      9  union all
     10  select '二月份', '04', 9 from dual
     11  union all
     12  select '三月份','03',8 from dual
     13  )
     14  select
     15  部门dep,
     16  decode(一月份,'','null',一月份) 一月份,
     17  decode(二月份,'','null',二月份) 二月份,
     18  decode(三月份,'','null',三月份) 三月份
     19  from
     20  (
     21  select 部门dep,
     22  max(decode(月份mon,'一月份' ,业绩yj)) 一月份,
     23  max(decode(月份mon,'二月份',业绩yj)) 二月份,
     24  max(decode(月份mon,'三月份',业绩yj)) 三月份
     25  FROM TT
     26  group by 部门dep
     27  order by 部门dep
     28  );
     
    部门DEP 一月份     二月份     三月份
    ------- ---------- ---------- ----------
    01      10         null       null
    02      10         8          null
    03      5          null       8
    04      null       9          null
     
      

  2.   

    with tt as(
      select '一月份' mon, '01' dep, 10 yj from dual union all
      select '一月份' mon, '02' dep, 10 yj from dual union all
      select '一月份' mon, '03' dep, 5  yj from dual union all
      select '二月份' mon, '02' dep, 8  yj from dual union all
      select '二月份' mon, '04' dep, 9  yj from dual union all
      select '三月份' mon, '03' dep, 8  yj from dual)
    SELECT dep,
           SUM(decode(mon, '一月份', yj)) 一月份,
           SUM(decode(mon, '二月份', yj)) 二月份,
           SUM(decode(mon, '三月份', yj)) 三月份
      FROM tt
     GROUP BY dep
     ORDER BY dep;
      

  3.   


    select t1.dep,dname,
    sum(case when mon='一月份' then yj else null end) 一月份,
    sum(case when mon='二月份' then yj else null end) 二月份,
    sum(case when mon='三月份' then yj else null end) 三月份
    from t1,t2
    where t1.dep=t2.dep
    group by t1.dep,dname
      

  4.   


    create table t1
    (
        mon varchar2(20),
        dep varchar2(10),
        yj  int
    );insert into t1
        values('Jan','01',10);
    insert into t1
        values('Jan','02',10);
    insert into t1
        values('Jan','03',5);    
    insert into t1
        values('Feb','02',8);
    insert into t1
        values('Feb','04',9);
    insert into t1
        values('Mar','03',8);   
      select dep,
        sum(case when mon = 'Jan' then yj else NULL end) as Jan,
        sum(case when mon = 'Feb' then yj else NULL end) as Feb,
        sum(case when mon = 'Mar' then yj else NULL end) as Mar
    from t1
    group by dep 
    order by dep /*
    DEP JAN FEB MAR
    1 01 10
    2 02 10 8
    3 03 5 8
    4 04   9
    */