表数据 编号 网点   类型   费用   
  1   建行    1      1000       
  2   农行    1      2000   
  3   建行    2      1000   
  4   农行    2      4000   
  5   交行    2      2000   
  6   农行    2      1000 
   
   转成
   
   网点   类型1户数    费用    类型2户数  费用    类型户数合计    合计费用
   建行     1           1000     1        1000         2            2000
   农行     1           2000     2        5000         3            7000
   交行     0             0      1        2000         1            2000这样的oracle sql语句怎么写  随便说声我的oracle是9i的    急啊。。

解决方案 »

  1.   

    create table tmp(
      code number,
      outlets varchar2(50),
      costType char(1),
      costVal number
    );insert into tmp
    select 1,'建行','1',1000 from dual
    union all
    select 2,'农行','1',2000 from dual
    union all
    select 3,'建行','2',1000 from dual
    union all
    select 4,'农行','2',4000 from dual
    union all
    select 5,'交行','2',2000 from dual
    union all
    select 6,'农行','2',1000 from dual;Commit;select outlets,
             sum(decode(costType,'1',1,0)) 类型1户数,
             sum(decode(costType,'1',costVal,0)) 类型1费用,
             sum(decode(costType,'2',1,0)) 类型2户数,
             sum(decode(costType,'2',costVal,0)) 类型2费用,
             count(1) 类型户数合计,
             sum(costVal) 合计费用
     from tmp group by outlets
    OUTLETS                                                 类型1户数      类型1费用
    -------------------------------------------------- ---------- ----------
         类型2户数      类型2费用     类型户数合计       合计费用
    ---------- ---------- ---------- ----------
    建行                                                          1       1000
             1       1000          2       2000
                                                                                    
    农行                                                          1       2000
             2       5000          3       7000
                                                                                    
    交行                                                          0          0
             1       2000          1       2000
                                                                                    3 rows selected.
      

  2.   

    --创建动态查询的存储过程
    create or replace procedure tmp_colToRow
     (
         data_set out SYS_REFCURSOR
     )
     AS
      v_sql     VARCHAR2(5000);
      v_tt       VARCHAR2(5000);
     begin
           v_sql := 'SELECT outlets 网点';
           for x in (select costType from tmp GROUP BY costType ORDER BY costType)
           loop
               v_tt := v_tt||',SUM(DECODE(costType,'''||to_char(x.costType)||''',1,0)) 类型'||to_char(x.costType)||'户数,SUM(DECODE(costType,'''||to_char(x.costType)||''',costVal,0)) 类型'||to_char(x.costType)||'费用';
           end loop;
           v_sql := v_sql || v_tt || ',count(1) 类型户数合计,sum(costVal) 合计费用 from tmp group by outlets';
          --DBMS_OUTPUT.PUT_LINE(v_sql);
          open  data_set for v_sql;
     end;--执行存储过程
    exec tmp_colToRow(:v);
      

  3.   

    select  网点 ,
    case when  类型   = '1' then  费用    else 0 end,
    case when  类型   = '2' then  费用    else 0 end,
    count(*),
    sum(费用)
    from 表
    group by  网点如果类型有重复的数据,把CASE 用SUM。
      

  4.   

    使用PIVOT方法,http://tongliqiang.iteye.com/blog/975417,希望能对你有帮助,如果类型是动态的话,可采用拼装SQL语句的办法,实现动态执行
      

  5.   

    PIVOT 和 UNPIVOT 是11g才支持的新功能
      

  6.   

    select 
    outlets,sum(case when costtype=1 then 1 else 0 end ) 类型1户数,
           sum (case when costtype =1 then costval else 0 end )费用,
           sum(case when costtype=2 then 1 else 0 end ) 类型2户数,
           sum (case when costtype =2 then costval else 0 end )费用,
           count(1),
           sum(costval) from tmp group by outlets