免费注册 查看新帖 |

Chinaunix

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

[其他] 这边高手比较多,请教一下sql语句整合简化的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-02-25 12:46 |只看该作者 |倒序浏览
表建立:
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-10-10 17:00:29辰龙
日期:2013-10-12 16:23:19卯兔
日期:2013-11-18 17:01:27金牛座
日期:2014-09-09 10:17:052015七夕节徽章
日期:2015-08-21 11:06:172015亚冠之柏太阳神
日期:2015-09-25 13:56:42数据库技术版块每日发帖之星
日期:2016-08-06 06:20:00
2 [报告]
发表于 2014-02-25 14:08 |只看该作者
LZ你来错了吧;
你第一条sql意思其实就是这个
  1. select b.xh1,b.xh2,sum(je) je1 from A a,B b where a.lsh=b.lsh and b.inum=10 group by b.xh1,b.xh2
复制代码
可是你的结果这样的话;
  1. select t1.je1,t2.je2 from
  2. (select b.xh1,b.xh2,sum(je) je1 from A a,B b where a.lsh=b.lsh and b.inum=10 group by b.xh1,b.xh2) t1
  3. full join
  4. (select xh1,xh2,sum(je) je2 from B where bwh='10100' group by xh1,xh2) t2
  5. on t1.xh1=t2.xh1 and t1.xh2=t2.xh2;
复制代码

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
3 [报告]
发表于 2014-02-25 16:19 |只看该作者
回复 2# li0924

撞枪口上了~
   

论坛徽章:
0
4 [报告]
发表于 2014-02-25 21:30 |只看该作者
回复 2# li0924

恩,这样可以实现。那不用join可以吗?比如下面的问题
http://bbs.chinaunix.net/thread-4126374-1-1.html


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP