一个多表统计多次GROUP BY 的问题

lbd8848 2008-06-30 04:43:15
有表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
...全文
466 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
oraclelogan 2008-07-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lbd8848 的回复:]
谢谢大家,正如doer_ljy所言单位是不定项,请问如何使用存储过程!
[/Quote]

学习下,我也正在学习存储过程啊。
fcsoft01 2008-07-02
  • 打赏
  • 举报
回复
java报表工具在: http://www.fcsoft.com.cn/webreport.htm
wangzepen 2008-07-02
  • 打赏
  • 举报
回复
http://www.zzGps.cn 在线技术视频

http://www.ZzGps.Cn/bbs/index.asp 技术视频下载

技术视频提供,分类清晰,技术覆盖面广,几千视频助您轻松学习
hebo2005 2008-07-01
  • 打赏
  • 举报
回复
lbd8848 2008-07-01
  • 打赏
  • 举报
回复
谢谢大家,正如doer_ljy所言单位是不定项,请问如何使用存储过程!
djfu 2008-07-01
  • 打赏
  • 举报
回复
楼上的贴那么一堆代码搞什么?
xueru9999 2008-07-01
  • 打赏
  • 举报
回复
比如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) AS

begin

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;

/
billlyh 2008-07-01
  • 打赏
  • 举报
回复
ding!!!!!!!!!!!!!!
wenjuan0921 2008-06-30
  • 打赏
  • 举报
回复
发错了
理解错误 汗
doer_ljy 2008-06-30
  • 打赏
  • 举报
回复
忘了grup by cpmc
doer_ljy 2008-06-30
  • 打赏
  • 举报
回复
如果你只有三个单位的话,倒是好办。
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
如果是不订数目的,建议还是使用存储过程
wenjuan0921 2008-06-30
  • 打赏
  • 举报
回复
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

如果部门只有几个的话,可以这么做,也只是个提示 你看看
hebo2005 2008-06-30
  • 打赏
  • 举报
回复
BBBBBBB近你的规律应该是
BBBBBBB 5.00 0.00 0.20
否则没法做的
你的排列顺序看上去应该是
千克     1吨=1*1000 千克 1克=1/1000千克
hebo2005 2008-06-30
  • 打赏
  • 举报
回复
你给的目标数据里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

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧