123
返回列表 发新帖
楼主: fuiou_cary

[原创] 【讨论】多字段or连接查询如何优化!

[复制链接]
论坛徽章:
12
优秀写手
日期:2014-09-03 06:00:13双鱼座
日期:2015-12-16 10:37:41天蝎座
日期:2015-12-02 14:44:59秀才
日期:2015-11-11 09:58:34ITPUB14周年纪念章
日期:2015-10-26 17:23:44巨蟹座
日期:2015-09-02 14:45:30金牛座
日期:2015-08-25 16:02:53天蝎座
日期:2015-07-21 16:49:04喜羊羊
日期:2015-07-07 16:42:312015年新春福章
日期:2015-03-06 11:59:47
21#
 楼主| 发表于 2014-8-26 15:31 | 只看该作者
mchdba 发表于 2014-8-26 12:55
如果要去重的功效,就直接用union 不要用union all就可以了。

我用的就是union啊,不知道为什么没去重呢,我写的是
select sum(t.counts),sum(t.txn_amount) from (select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and tm_agent_1 ='08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and tm_agent_2 ='08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and tm_agent_3 = '08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and agent_cd_1 = '08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and agent_cd_2 = '08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and agent_cd_3='08A000S000'
union
select count(1) as counts, sum(src_amt) as txn_amount from T_06
where fas_settle_dt>= '20140601' and fas_settle_dt <= '20140630' and tm_ins='08A000S000') t;
说明一下啊,tm_agent_1,tm_agent_2,tm_agent_3,agent_cd_1,agent_cd_2,agent_cd_3,tm_ins这些字段不是只有一个有值,大部分时候有几个字段同时取一个值,少数情况下是只有一个字段取值

使用道具 举报

回复
论坛徽章:
12
优秀写手
日期:2014-09-03 06:00:13双鱼座
日期:2015-12-16 10:37:41天蝎座
日期:2015-12-02 14:44:59秀才
日期:2015-11-11 09:58:34ITPUB14周年纪念章
日期:2015-10-26 17:23:44巨蟹座
日期:2015-09-02 14:45:30金牛座
日期:2015-08-25 16:02:53天蝎座
日期:2015-07-21 16:49:04喜羊羊
日期:2015-07-07 16:42:312015年新春福章
日期:2015-03-06 11:59:47
22#
 楼主| 发表于 2014-8-26 15:52 | 只看该作者
fuiou_cary 发表于 2014-8-26 15:31
我用的就是union啊,不知道为什么没去重呢,我写的是
select sum(t.counts),sum(t.txn_amount) from (se ...

知道为什么了,每个union取的是count不是具体字段,取明细就会去重了

使用道具 举报

回复
论坛徽章:
32
ITPUB十周年纪念徽章
日期:2011-11-01 16:25:22马上加薪
日期:2014-12-08 22:53:43马上有房
日期:2014-12-09 01:05:19美羊羊
日期:2015-03-04 14:52:282015年新春福章
日期:2015-03-06 11:58:18巨蟹座
日期:2015-11-17 11:02:55双子座
日期:2015-11-17 12:21:47白羊座
日期:2015-12-10 17:27:08狮子座
日期:2016-02-23 10:11:01双子座
日期:2016-02-23 10:17:05
23#
发表于 2014-12-8 22:52 | 只看该作者
可以用IN替代

使用道具 举报

回复
论坛徽章:
27
2014年新春福章
日期:2014-02-18 16:50:09秀才
日期:2015-06-25 15:39:28秀才
日期:2015-06-29 15:26:52秀才
日期:2015-07-03 17:00:53秀才
日期:2015-07-14 09:44:30金牛座
日期:2015-07-21 14:01:36秀才
日期:2015-09-14 10:08:30秀才
日期:2016-02-18 09:24:18秀才
日期:2016-02-18 10:08:02举人
日期:2016-02-24 17:33:17
24#
发表于 2014-12-23 22:43 | 只看该作者
or 找成 union all

使用道具 举报

回复
论坛徽章:
22
ITPUB 11周年纪念徽章
日期:2012-10-09 18:16:00马上加薪
日期:2014-10-21 18:48:25马上加薪
日期:2014-10-21 18:48:312015年新春福章
日期:2015-03-04 14:53:162015年新春福章
日期:2015-03-06 11:58:39沸羊羊
日期:2015-06-11 17:08:14巨蟹座
日期:2015-07-10 09:11:44天枰座
日期:2016-01-18 10:58:39秀才
日期:2016-02-18 10:08:14秀才
日期:2016-06-23 14:15:06
25#
发表于 2014-12-24 08:50 | 只看该作者
本帖最后由 stevendba 于 2014-12-24 08:51 编辑

表设计不对,(tm_agent_1 ='08M082'
       or tm_agent_2 ='08M082'
       or tm_agent_3 = '08M082'
       or agent_cd_1 = '08M082'
       or agent_cd_2 = '08M082'
       or agent_cd_3='08M082'
       or tm_ins='08M082')
这些字段要单独提取出来,然后加一个标志位,然后用表关联。
http://blog.csdn.net/stevendbaguo/article/details/39472273

使用道具 举报

回复

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

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表