简体   繁体   English

Oracle SQL - 什么比3“更快并且存在()”在哪里?

[英]Oracle SQL - What is faster than 3 “and exists()” in the where space?

Is there a better way to write 有没有更好的写作方式

Select distinct id_no
from revenue_table
where (exists (select * from revenue_table i 
       where revenue_type = 'Shipping' and i.id_no = r.id_no)
  and exists(select * from revenue_table i 
       where revenue_type = 'Reproduction' and i.id_no = r.id_no)
  and exists(select * from revenue_table i 
       where revenue_type = 'Tape' and i.id_no = r.id_no))

id_no represents a form, which is entered into the table once for each revenue item on the form. id_no表示一个表单,该表单为表单上的每个收入项输入一次。 Same revenue_type's can appear multiple times. 相同的revenue_type可以多次出现。 There are many advanced functions that work using OR logic but I cannot seem to find any functions using AND set theory. 有许多使用OR逻辑的高级函数,但我似乎无法使用AND集理论找到任何函数。 It would be really nice if GROUP BY had some function to compare a group such as id_no to a set like (Shipping, Reproduction, Tape) 如果GROUP BY有一些功能可以将像id_no这样的组比较为(装运,复制,磁带)这样的集合,那将是非常好的。

Does this exist? 这存在吗?

Another option would be 另一种选择是

SELECT id_no
FROM   revenue_table
WHERE  revenue_type IN ( 'Tape', 'Shipping', 'Reproduction' )
GROUP  BY id_no
HAVING COUNT(DISTINCT revenue_type) = 3  

You'll have to test whether or not it is an improvement. 你必须测试它是否是一种改进。

A number of relational operators imply logical AND eg join, restriction, extension, intersection. 许多关系运算符意味着逻辑AND,例如连接,限制,扩展,交集。

SQL's INTERSECT would be appropriate here eg SQL的INTERSECT在这里是合适的,例如

SELECT id_no
  FROM revenue_table
 WHERE revenue_type = 'Shipping' 
INTERSECT
SELECT id_no
  FROM revenue_table
 WHERE revenue_type = 'Reproduction' 
INTERSECT
SELECT id_no
  FROM revenue_table
 WHERE revenue_type = 'Tape';

Yes, you can do it with INNER JOIN : 是的,您可以使用INNER JOIN执行此操作:

select distinct r.id_no 
from revenue_table r
inner join revenue_table i1 on i1.id_no = r.id_no and i1.revenue_type = 'Shipping'
inner join revenue_table i2 on i2.id_no = r.id_no and i2.revenue_type = 'Reproduction'
inner join revenue_table i3 on i3.id_no = r.id_no and i3.revenue_type = 'Tape'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM