有表A(单位表)
编号(bh)    单位名称(dwmc)
----------------------
001         a单位
002         b单位
003         c单位表B(数据表)
dwbh(单位编号)   cpmc(产品名称)  sl(数量)  jldw(计量单位三种,吨 千克 克) 
-------------------------------------------------------------------
002               AAAAAAA        1       吨
003               BBBBBBB        5       千克
001               CCCCCCC        10      千克
002               BBBBBBB        200     克
002               CCCCCCC        50      吨====================================
如何构建统计查询得到如下结果cpmc(产品名称)       dwmc1(单位001)           dwmc2(单位002)    dwmc3(单位003) ......
-------------------------------------------------------------------------------
AAAAAAA             0.00                         1000.00             0.00
BBBBBBB             0.00                         0.20                5.00
CCCCCCC             10.00                        50000.00            0.00

解决方案 »

  1.   

    你给的目标数据里BBBBBBB有错误,0.2和5应该换一下
    select a.dwmc cpmc,
           max(decode(jldw,'千克'),sl,0)  dwmc1,
           max(decode(jldw,'吨'),sl*1000,0)  dwmc2,
           max(decode(jldw,'克'),sl/1000,0)  dwmc3,
        from a,b
         where a.bh=b.dwbh
         group by a.dwmc 
      

  2.   

    BBBBBBB近你的规律应该是
    BBBBBBB            5.00        0.00                0.20                
    否则没法做的
    你的排列顺序看上去应该是
    千克     1吨=1*1000 千克     1克=1/1000千克
      

  3.   

    select cpmc(产品名称),
    sum(case when dwbh='001'then sl else 0 end) part1,
    sum(case when dwbh='002'then sl else 0 end) part2,
    sum(case when dwbh='003'then sl else 0 end) part3
    from 表2 group by cpmc
    如果部门只有几个的话,可以这么做,也只是个提示 你看看 
      

  4.   

    如果你只有三个单位的话,倒是好办。
    select cpmc,
    sum(decode(dwbh,'001',decode(jldw,'千克',sl,decode(jlwd,'吨',sl*1000,sl/1000)),0)) dw1,
    sum(decode(dwbh,'002',decode(jldw,'千克',sl,decode(jlwd,'吨',sl*1000,sl/1000)),0)) ,dw2
    sum(decode(dwbh,'003',decode(jldw,'千克',sl,decode(jlwd,'吨',sl*1000,sl/1000)),0)) dw3
    from b
    如果是不订数目的,建议还是使用存储过程
      

  5.   

    谢谢大家,正如doer_ljy所言单位是不定项,请问如何使用存储过程!
      

  6.   

    非固定列,参考此贴吧
    http://topic.csdn.net/u/20080416/11/910e40c1-60f1-441f-8b0f-19a969d30f77.html
      

  7.   

    比如emp中有这样的纪录: ID NAME DEPT_ID SALARY 001 a01 b01 1000 002 a02 b02 2000 003 a03 b03 3000 经过转换之后,表emp_rc中应该是这样的: COL_1 COL_2 COL_3 COL_4 ID 001 002 003 NAME a01 a02 a03 DEPT_ID b01 b02 b03 SALARY 1000 2000 3000 **************************************************** 源代码如下: CREATE OR REPLACE procedure ZBTOWN.TRANSFER_RC(tb_in varchar,tb_out varchar) ASbegin     declare            cnt number;            arr_cnt number;            select_cmd varchar2(1000) := '';            create_cmd varchar2(1000) := '';            insert_cmd varchar2(1000) := '';            type arr_type is table of varchar2(100) index by binary_integer;            arr arr_type;            type refcursor is ref cursor;            ref_cv refcursor;            cursor c_cols is                  select column_name                  from user_tab_columns                  where table_name = upper(tb_in);            r_cols c_cols%rowtype;     begin           arr_cnt := 1;          -- drop            begin            execute immediate 'drop table ' || tb_out;           exception when others then             null;             end;          -- create          begin            select_cmd := 'select count(0) as cnt from ' || tb_in;            execute immediate select_cmd into cnt;            create_cmd := 'create table ' || tb_out || '(';            for t in 1..cnt+1 loop                create_cmd := create_cmd || ' col_'|| t || ' varchar2(100),';            end loop;            create_cmd := create_cmd || 'constraint ' || tb_out || '_pk primary key(col_1) using index)';            execute immediate create_cmd;           exception when others then             null;          end;          -- insert          begin            for r_cols in c_cols loop                exit when c_cols%notfound;                insert_cmd := 'insert into '|| tb_out ||' values(';                arr(arr_cnt) := r_cols.column_name;                insert_cmd := insert_cmd || '''' || arr(arr_cnt) || ''',';                select_cmd := 'select ' || r_cols.column_name || ' from ' || tb_in;                open ref_cv for select_cmd;                loop                    arr_cnt := arr_cnt + 1;                    fetch ref_cv into arr(arr_cnt);                    exit when ref_cv%notfound;                    insert_cmd := insert_cmd || '''' || arr(arr_cnt) || ''',';                end loop;                insert_cmd := substr(insert_cmd,1,length(insert_cmd)-1) || ')';                execute immediate insert_cmd;            end loop;         exception when others then           null;          end;          -- last commit          commit;     end;end;/
      

  8.   

    http://www.zzGps.cn 在线技术视频 http://www.ZzGps.Cn/bbs/index.asp 技术视频下载 技术视频提供,分类清晰,技术覆盖面广,几千视频助您轻松学习
      

  9.   

    java报表工具在: http://www.fcsoft.com.cn/webreport.htm