MINUS Operator in oracle

喜你入骨 提交于 2019-11-30 23:10:06

问题


MINUS Operator

I have 2 tables A and B.

SELECT COUNT(*) FROM (SELECT * FROM tableA)

returns 389

SELECT COUNT(*) FROM (SELECT * FROM tableB)

returns 217

SELECT COUNT(*) FROM
(SELECT * FROM tableA
INTERSECT
SELECT * FROM tableB)

returns 0

SELECT COUNT(*) FROM 
(SELECT * FROM tableA
MINUS
SELECT * FROM tableB)

returns 389

SELECT COUNT(*) FROM 
(SELECT * FROM tableB
MINUS
SELECT * FROM tableA)

retuns 89

Can someone please explain why does the last query return 89 and not 217?


回答1:


MINUS takes the first result set, and removes any that exist in the second result set; it also removes any duplicates.

In your example, tableA has 389 rows, and tableB has 217 rows; your INTERSECT shows there are no rows in common, which means tableA MINUS tableB is 389 rows (i.e. all of them).

tableB MINUS tableA returns the distinct set of rows in tableB, therefore there are 89 distinct values in tableB.




回答2:


Suppose if you have set A and B, A = {1,2,3,4} and count(A) = 4, B = {5,6,6,7,7} and count(B) = 5

A-B = {1,2,3,4} thus count(A-B) = count(A) = 4

But B-A = {5,6,7} and count(B) = 3

Thus what we understand here is that minus eliminates the duplicate terms(or rows). That's the reason why the row count reduced from 217 to 89.

Hope this helps.



来源:https://stackoverflow.com/questions/20209265/minus-operator-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!