Please read
http://www.dbonline.cn/source/oracle/20031215/BACKUP_transposing%20a%20table%
20into%20a%20summary%20matrix.html

解决方案 »

  1.   

    如果数据不多,可以用decode函数来做,如果数据比较,只有写存储过程了
      

  2.   

    想到一种很病的方法。大侠们,帮我简化一下下面的SQL语句,谢谢!select '目标数' as xm,(
    select count(*)  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') and fb_type='1' and fb_subtype1<>'T') and khpp='0' ) as qqt,
       (select count(*)  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') 
    and fb_type='1' and fb_subtype1<>'T') and khpp='2' ) as dgdd,

    (select count(*)  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') 
    and fb_type='1' and fb_subtype1<>'T') and khpp='1' ) as szx,

    (select count(*)  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') 
    and fb_type='1' and fb_subtype1<>'T' ) and khpp in('3','4' )) as dfpp
    from dual
    union
    select '到达数' as xm,(
    select count(*)  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') 
    and fb_type='1' and fb_subtype1<>'T' and deal_status='H') and khpp='0' ) as qqt,

    (select count(*)  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') 
    and fb_type='1' and fb_subtype1<>'T' and deal_status='H') and khpp='2' ) as dgdd,

    (select count(*)  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') 
    and fb_type='1' and fb_subtype1<>'T' and deal_status='H') and khpp='1' ) as szx,

    (select count(*)  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') 
    and fb_type='1' and fb_subtype1<>'T' and deal_status='H') and khpp in('3','4' ))as dfpp
    from dual;
      

  3.   

    --先把你的这两列取出插入临时表:insert into t# as ......
    --从临时表取你要的数据
    select (case khpp when 0 then count(*)0 when 1 then count(*)1 when 2 then count(*)2 when 3 then count(*)3 when 4 then count(*)4 ) from t#
      

  4.   

    jackjingsg(飞翔的精灵),是一种办法,能详细点吗?
    临时表:能不能这样我用的时候创建,不用的时候删除,如果这样,那么频繁的操作会对数据库有影响的。
      

  5.   

    表tablename khpp    ct  
    0 82
    1 54
    2 150
    3 1244
    4 231select 
        sum(col0) col0,
        sum(col1) col1,
        sum(col2) col2,
        sum(col3) col3,
        sum(col4) col4
    from 
    (
    select 
        decode(a.khpp,0,a.ct,0) col0,
        decode(a.khpp,1,a.ct,0) col1,
        decode(a.khpp,2,a.ct,0) col2,
        decode(a.khpp,3,a.ct,0) col3,
        decode(a.khpp,4,a.ct,0) col4
    from 
        tablename a
    )
      

  6.   

    --按正常的方式取出插入临时表:
    create table t# as  select khpp,count(*)  count  from feedback_info
    where serial_id in (
    select serial_id from customer_feedback 
    where fb_time>to_date('20040601','yyyymmdd') and fb_time<to_date('20040701','yyyymmdd') and fb_type='1' and fb_subtype1<>'T') group by khpp;--从临时表取出数据select (case khpp when 0 then count 0 when 1 then count 1 when 2 then count 2 when 3 then count 3 when 4 then count 4 ) from t#--删除临时表drop table t#;