免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5161 | 回复: 3
打印 上一主题 下一主题

[求助] sql语句简化整合?如何把3个sql语句整合成1条sql语句 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-02-25 12:45 |只看该作者 |倒序浏览
表建立:
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条语句实现?

论坛徽章:
7
天蝎座
日期:2013-08-16 23:19:32丑牛
日期:2014-01-08 09:20:14寅虎
日期:2014-01-11 11:03:44午马
日期:2014-04-28 11:02:40天秤座
日期:2014-05-16 23:24:24摩羯座
日期:2014-07-20 10:46:04卯兔
日期:2014-08-08 15:21:41
2 [报告]
发表于 2014-02-25 20:43 |只看该作者
union,jion,子查询等方式都可以

论坛徽章:
0
3 [报告]
发表于 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;


你全部嵌套在一起不就行了,多简单

论坛徽章:
7
天蝎座
日期:2013-08-16 23:19:32丑牛
日期:2014-01-08 09:20:14寅虎
日期:2014-01-11 11:03:44午马
日期:2014-04-28 11:02:40天秤座
日期:2014-05-16 23:24:24摩羯座
日期:2014-07-20 10:46:04卯兔
日期:2014-08-08 15:21:41
4 [报告]
发表于 2014-02-28 20:43 |只看该作者
学习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP