标题: sql语句简化整合?如何把3个sql语句整合成1条sql语句 [打印本页] 作者: ryr1990 时间: 2014-02-25 12:45 标题: sql语句简化整合?如何把3个sql语句整合成1条sql语句 表建立:
table A
(
lsh char(20),
je number
)
table B
(
xh1 char(10),
xh2 char(10),
lsh char(20),
je number,
inum int,
bwh char(10),
)
1.建立临时表1
select xh1,xh2,sum(je) je1 from
(
select b.xh1,b.xh2,a.je
from
(select je from A where lsh in (select lsh from B where inum=10)) a,
B b
where a.lsh=b.lsh
)
group by xh1,xh2 into temp tmp_table1;
2.建立临时表2
select xh1,xh2,sum(je) je2 from B where bwh='10100' group by xh1,xh2 into temp tmp_table2;
3.临时表做并集
select a.je1,b.je2 from tmp_table1 a,tmp_table2 b where a.xh1=b.xh1 and a.xh2=b.xh2;
(
select xh1,xh2,sum(je) je1 from
(
select b.xh1,b.xh2,a.je
from
(select je from A where lsh in (select lsh from B where inum=10)) a,
B b
where a.lsh=b.lsh
)
group by xh1,xh2)
a,(
select xh1,xh2,sum(je) je2 from B where bwh='10100' group by xh1,xh2
) ab