Chinaunix

标题: 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;

请问有什么好的方法把上面3个语句整合到1条语句实现?
作者: www_xylove    时间: 2014-02-25 20:43
union,jion,子查询等方式都可以

作者: dingning239    时间: 2014-02-26 15:38
select a.je1,b.je2 from

(
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

where a.xh1=b.xh1 and a.xh2=b.xh2;


你全部嵌套在一起不就行了,多简单
作者: www_xylove    时间: 2014-02-28 20:43
学习。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2