12
返回列表 发新帖
楼主: kigo6

[SQL] minus SQL 優化請教

[复制链接]
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
11#
发表于 2017-5-29 21:25 | 只看该作者

可以把having精简:
having count(DECODE(systype,'a',null,1))=0

使用道具 举报

回复
论坛徽章:
169
SQL数据库编程大师
日期:2016-01-13 10:30:43SQL极客
日期:2013-12-09 14:13:35SQL大赛参与纪念
日期:2013-12-06 14:03:45最佳人气徽章
日期:2015-03-19 09:44:03现任管理团队成员
日期:2015-08-26 02:10:00秀才
日期:2015-07-28 09:12:12举人
日期:2015-07-13 15:30:15进士
日期:2015-07-28 09:12:58探花
日期:2015-07-28 09:12:58榜眼
日期:2015-08-18 09:48:03
12#
发表于 2017-5-29 22:50 | 只看该作者
newkid 发表于 2017-5-29 21:25
可以把having精简:
having count(DECODE(systype,'a',null,1))=0

腻害,确实如此!精简了不少,反正where 里面已经有了dept_id,然后又少了b,c,d......

使用道具 举报

回复
论坛徽章:
4
ITPUB 11周年纪念徽章
日期:2012-10-09 18:17:012014年新春福章
日期:2014-02-18 16:44:08马上有对象
日期:2014-02-18 16:44:08优秀写手
日期:2014-10-31 06:00:13
13#
 楼主| 发表于 2017-5-30 03:11 | 只看该作者

剛剛多做了個試驗 如果同一systype 但dept不一樣的話,結果卻不一樣了
  
原SQL:
select item_id from table a where systype ='a' and dept_id <> '1234'
minus
select item_id from table a where systype ='a' and dept_id = '1234'
minus
select item_id from table a where systype ='b' and dept_id <> '1234'
minus
select item_id from table a where systype ='c' and dept_id <> '1234'

改寫後:
select item_id from table a
    where  a.systype in('a','b','c')
   group by item.id
    having count(case when systype='a' and dept_id='1234' then 1 else null end )=0
     having count(case when systype='b' and dept_id<>'1234' then 1 else null end )=0
     having count(case when systype='c' and dept_id<>'1234' then 1 else null end )=0
   

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
14#
发表于 2017-5-30 04:08 | 只看该作者
你这13楼的需求和7楼又不一样了,然后套用别人对7楼的解答,这完全是风马牛不相及。
建议:
“想清楚”自己要干什么。
“说清楚”自己要干什么。
给出建表、INSERT语句构造出一个测试用例。
给出预期结果。如果自己已有查询,贴出来并说明为什么不是你想要的结果。
其实,就是参见置顶帖“提问的智慧”。懂得提问的人,问题很快就能得到解答。

使用道具 举报

回复
论坛徽章:
4
ITPUB 11周年纪念徽章
日期:2012-10-09 18:17:012014年新春福章
日期:2014-02-18 16:44:08马上有对象
日期:2014-02-18 16:44:08优秀写手
日期:2014-10-31 06:00:13
15#
 楼主| 发表于 2017-5-30 05:11 | 只看该作者
本帖最后由 kigo6 于 2017-5-30 05:16 编辑
newkid 发表于 2017-5-30 04:08
你这13楼的需求和7楼又不一样了,然后套用别人对7楼的解答,这完全是风马牛不相及。
建议:
“想清楚”自 ...

了解,其實9樓與11樓已經是我現階段的需求了

13樓的問題單純是我一時發想的問題,想說可以一起討論...

謝謝newkid提醒,未來針對問題的發問會更謹慎
=========================================

sql:

create table iteminfo (item_id varchar2(10),systype varchar2(10),dept_id varchar2(10));

insert into iteminfo values ('it_1','a','1234');
insert into iteminfo values ('it_2','a','2234');
insert into iteminfo values ('it_3','a','3456');
insert into iteminfo values ('it_4','a','1325');
insert into iteminfo values ('it_5','b','1234');
insert into iteminfo values ('it_6','b','2234');
insert into iteminfo values ('it_7','b','3456');
insert into iteminfo values ('it_8','b','1325');
insert into iteminfo values ('it_9','c','1234');
insert into iteminfo values ('it_10','c','2234');
insert into iteminfo values ('it_11','c','3456');
insert into iteminfo values ('it_12','c','1325');

commit;


SQL> select item_id from iteminfo where systype='a' and dept_id<>'1234'
  2  minus
  3  select item_id from iteminfo where systype='a' and dept_id='1234'
  4  minus
  5  select item_id from iteminfo where systype='b' and dept_id<>'1234'
  6  minus
  7  select item_id from iteminfo where systype='c' and dept_id<>'1234'
  8    ;

ITEM_ID
----------
it_2
it_3
it_4

SQL> select item_id from iteminfo
  2   where systype in ('a','b','c')
  3    group by item_id
  4    having count(case when systype='a' and dept_id='1234' then 1 else null end )=0
  5    and count(case when systype='b' and dept_id<>'1234' then 1 else null end )=0
  6    and count(case when systype='c' and dept_id<>'1234' then 1 else null end )=0
  7  ;

ITEM_ID
----------
it_2
it_3
it_4
it_5
it_9

SQL> spool off


使用道具 举报

回复
论坛徽章:
4
ITPUB 11周年纪念徽章
日期:2012-10-09 18:17:012014年新春福章
日期:2014-02-18 16:44:08马上有对象
日期:2014-02-18 16:44:08优秀写手
日期:2014-10-31 06:00:13
16#
 楼主| 发表于 2017-5-30 05:27 | 只看该作者
本帖最后由 kigo6 于 2017-5-30 05:37 编辑
kigo6 发表于 2017-5-30 05:11
了解,其實9樓與11樓已經是我現階段的需求了

13樓的問題單純是我一時發想的問題,想說可以一起討論 .. ...

看我這蠢的...
到最後研究發現 原來是where過程中 忘記先將其他systype給濾了

result 1:
select item_id from item_info
where (systype ='a') or (systype='b' and dpet<> '1234') or (systype='c' and dept<>'1234')
group by item_id
having count(case when systype='a' and dept='1234' then 1 else null end)=0
count(case when systype='b' and dept<>'1234' then 1 else null end)=0
count(case when systype='c' and dept<>'1234' then 1 else null end)=0

result 2:
select item_id from item_info
where (systype ='a') or (systype='b' and dpet<> '1234') or (systype='c' and dept<>'1234')
group by item_id
having count(decode(systype,'a',null,1))=0 and  count(decode(dept_id,'1234',1,null)) =0

使用道具 举报

回复

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

本版积分规则 发表回复

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