请教group by!
table1
name sl
---- ---
aaa 3
bbb 2
ccc 5
aaa 2
fff 7
bbb 1
table2:
name sl
----- ----
aaa 1
fff 6
怎样得出这样的结果:
name sl_1 sl_2 sl3
----- ---- ---- ----
aaa 5 1 4
bbb 3 0 0
ccc 5 0 0
fff 7 6 1
先谢了!
问题点数:0、回复次数:8Top
1 楼delian(仲秋)回复于 2003-09-02 00:12:07 得分 0
写错了,更改为:
table1
name sl
---- ---
aaa 3
bbb 2
ccc 5
aaa 2
fff 7
bbb 1
table2:
name sl
----- ----
aaa 1
fff 6
aaa 1
怎样得出这样的结果:
name sl_1 sl_2 sl3
----- ---- ---- ----
aaa 5 2 3
bbb 3 0 3
ccc 5 0 5
fff 7 6 1
Top
2 楼delian(仲秋)回复于 2003-09-02 00:13:05 得分 0
写错了,更改为:
table1
name sl
---- ---
aaa 3
bbb 2
ccc 5
aaa 2
fff 7
bbb 1
table2:
name sl
----- ----
aaa 1
fff 6
aaa 1
怎样得出这样的结果:
name sl_1 sl_2 sl3
----- ---- ---- ----
aaa 5 2 3
bbb 3 0 3
ccc 5 0 5
fff 7 6 1
Top
3 楼beckhambobo(beckham)回复于 2003-09-02 09:36:26 得分 0
select name,s1_1,s1_2,(s1_1-s1_2) sl3 from
(select name,sum(s1) s1_1,(select sum(s1) from table2 where name=table1.name) sl_2 from table1 group by name)Top
4 楼delian(仲秋)回复于 2003-09-02 11:54:15 得分 0
to: beckhambobo(beckham)
我实际的两张表是这样的:
A表数据:
品牌 型号 规格 数量 单价 合计
10001 10003 100010 3 11300 33900
10001 10003 100010 1 11200 11200
10007 10019 100042 1 1 1
10007 10023 100046 11 23 253
10001 10003 100018 2 10200 20400
10001 10003 100027 4 11500 46000
B表数据
品牌 型号 规格 数量 单价 合计
10001 10003 100010 1 12000 12000
10001 10003 100010 1 12500 12500
10001 10003 100010 1 12000 12000
10007 10019 100042 1 3 3
10007 10023 100046 2 30 60
要得出的结果是:
品牌 型号 规格 A数量 B数量 库存数量(A数量 - B数量)
10001 10003 100010 4 3 1
10001 10003 100018 2 0 2Top
5 楼beckhambobo(beckham)回复于 2003-09-02 12:18:41 得分 0
select 品牌,型号,规格,s1_1,s1_2,(s1_1-s1_2) sl3 from
(select 品牌,型号,规格,sum(数量) s1_1,(select sum(数量) from table2 where 品牌=table1.品牌 and 型号=table1.型号 and 规格=table1.规格) sl_2 from table1 group by 品牌,型号,规格)
Top
6 楼delian(仲秋)回复于 2003-09-02 13:13:30 得分 0
TO: beckhambobo(beckham)
这样写报错:
select cpmc,ppmc,xhmc,s1_1,s1_2,(s1_1-s1_2) sl3 from
(select cpmc,ppmc,xhmc,sum(rksl) s1_1,(select sum(cksl) from v_kc_ckbb01 where
cpmc=v_kc_rkbb01.cpmc and ppmc=v_kc_rkbb01.ppmc and xhmc=v_kc_rkbb01.xhmc) sl_2
from v_kc_rkbb01 group by cpmc,ppmc,xhmc);
执行后;
连接到:
Oracle8 Enterprise Edition Release 8.0.5.0.0 - Production
PL/SQL Release 8.0.5.0.0 - Production
SQL> select cpmc,ppmc,xhmc,s1_1,s1_2,(s1_1-s1_2) sl3 from
2 (select cpmc,ppmc,xhmc,sum(rksl) s1_1,(select sum(cksl) from v_kc_ckbb01 where
3 cpmc=v_kc_rkbb01.cpmc and ppmc=v_kc_rkbb01.ppmc and xhmc=v_kc_rkbb01.xhmc) sl_2
4 from v_kc_rkbb01 group by cpmc,ppmc,xhmc);
(select cpmc,ppmc,xhmc,sum(rksl) s1_1,(select sum(cksl) from v_kc_ckbb01 where
*
错误位于第2行:
ORA-00936: 缺少表达式
Top
7 楼beckhambobo(beckham)回复于 2003-09-02 14:36:26 得分 0
select 品牌,型号,规格,s1_1,s1_2,(s1_1-s1_2) sl3 from
(select 品牌,型号,规格,sum(a.数量) s1_1,sum(b.数量) sl_2 from table1 a,table2 b where a.品牌=b.品牌(+) and a.型号=b.型号(+) and a.规格=b.规格(+) group by 品牌,型号,规格)
Top
8 楼zbhtj(钢骨黑布伞)回复于 2003-09-03 14:49:31 得分 0
select a.name,s1_1,nvl(s1_2,0) , (s1_1-nvl(s1_2,0)) s3
from
(select name,sum(s1) s1_1
from aaa
group by name) a,
(select a1.name,sum(bbb.s1) s1_2
from bbb,(select distinct name from aaa ) a1
where a1.name=bbb.name(+)
group by a1.name) b
where a.name=b.nameTop




