Home » SQL & PL/SQL » SQL & PL/SQL » Use of set in Oracle (Oracle 10g)
Use of set in Oracle [message #630091] Sat, 20 December 2014 02:38 Go to next message
sss111ind
Messages: 636
Registered: April 2012
Location: India
Senior Member

Hi All,

CREATE TABLE temp1(data1 VARCHAR2(1));

create table temp2(data1 varchar2(1));


INSERT INTO temp1 VALUES('A');
INSERT INTO temp1 VALUES('B');
INSERT INTO temp1 VALUES('C');
INSERT INTO temp1 VALUES('D');
INSERT INTO temp1 VALUES('E');

INSERT INTO temp2 VALUES('C');
INSERT INTO temp2 VALUES('D');
INSERT INTO temp2 VALUES('E');
INSERT INTO temp2 VALUES('F');
INSERT INTO temp2 VALUES('G');

I want TO SELECT DATA WHICH IS NOT COMMON TO EITHER OF THE TABLE AS 
--A B F G

existing A b c d e
added    f g c d e


Regards,
Nathan

[Updated on: Sat, 20 December 2014 02:41]

Report message to a moderator

Re: Use of set in Oracle [message #630092 is a reply to message #630091] Sat, 20 December 2014 03:05 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
You could use a compound query, using the MINUS and UNION ALL.
Re: Use of set in Oracle [message #630096 is a reply to message #630091] Sat, 20 December 2014 04:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
As your title mention "set" John's answer is the correct one.
An alternative can be found using a Join.

Re: Use of set in Oracle [message #630098 is a reply to message #630096] Sat, 20 December 2014 05:18 Go to previous messageGo to next message
sss111ind
Messages: 636
Registered: April 2012
Location: India
Senior Member

This might be one solution.
SELECT * FROM
  ( SELECT * FROM TEMP1
  UNION
  SELECT * FROM TEMP2
  )
MINUS
SELECT * FROM
  (SELECT * FROM TEMP2
  INTERSECT
  SELECT * FROM TEMP1
  );
Re: Use of set in Oracle [message #630100 is a reply to message #630098] Sat, 20 December 2014 05:30 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
I was thinking of this,
 select * from
(select * from temp1
minus
select * from temp2)
union all
(select * from temp2
minus
select * from temp1);
If you SET AUTOTRACE ON EXPLAIN and then run your satement and mine, you'll see the differences in execution plan. I think my vesion is slightly more efficient. Can you work out why?
Do you think your query needs UNION, or would UNION ALL be better?
Re: Use of set in Oracle [message #630102 is a reply to message #630100] Sat, 20 December 2014 05:50 Go to previous messageGo to next message
sss111ind
Messages: 636
Registered: April 2012
Location: India
Senior Member

Thanks john for pointing out that, really union is taking more cpu than union all. But how to do this with join as per Michel's suggestion.
Re: Use of set in Oracle [message #630103 is a reply to message #630102] Sat, 20 December 2014 05:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Just click on the link I gave you.

Re: Use of set in Oracle [message #630136 is a reply to message #630100] Sat, 20 December 2014 16:29 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3312
Registered: January 2010
Location: Connecticut, USA
Senior Member
John Watson wrote on Sat, 20 December 2014 06:30
I was thinking of this


This might be simpler:

WITH T AS (
            SELECT DATA1,1 FLAG FROM TEMP1
           UNION ALL
            SELECT DATA1,2 FLAG FROM TEMP2
          )
SELECT  DATA1
  FROM  T
  GROUP BY DATA1
  HAVING COUNT(DISTINCT FLAG) = 1
/


But first OP needs to explain "NOT COMMON TO EITHER OF THE TABLE". If TEMP1 is A, A, B, B, C, C, D, D, E, E and TEMP2 is C, D, E, F, F, G, G. Is "not common" A, B, F, G or A, A, B, B, C, D, E, F, F, G, G. Anyway, for former:

SQL> SELECT DATA1 FROM TEMP1 ORDER BY DATA1
  2  /

D
-
A
A
B
B
C
C
D
D
E
E

10 rows selected.

SQL> SELECT DATA1 FROM TEMP2 ORDER BY DATA1
  2  /

D
-
C
D
E
F
F
G
G

7 rows selected.

SQL> WITH T AS (
  2              SELECT DATA1,1 FLAG FROM TEMP1
  3             UNION ALL
  4              SELECT DATA1,2 FLAG FROM TEMP2
  5            )
  6  SELECT  DATA1
  7    FROM  T
  8    GROUP BY DATA1
  9    HAVING COUNT(DISTINCT FLAG) = 1
 10  /

D
-
A
B
G
F

SQL>


And for latter:

SQL> WITH T AS (
  2              SELECT DATA1,ROW_NUMBER() OVER(PARTITION BY DATA1 ORDER BY 1) FLAG FROM TEMP1
  3             UNION ALL
  4              SELECT DATA1,ROW_NUMBER() OVER(PARTITION BY DATA1 ORDER BY 1) FLAG FROM TEMP2
  5            )
  6  SELECT  DATA1
  7    FROM  T
  8    GROUP BY DATA1,
  9             FLAG
 10    HAVING COUNT(*) = 1
 11    ORDER BY DATA1
 12  /

D
-
A
A
B
B
C
D
E
F
F
G
G

11 rows selected.

SQL>


SY.

[Updated on: Sat, 20 December 2014 18:05]

Report message to a moderator

Re: Use of set in Oracle [message #630167 is a reply to message #630136] Mon, 22 December 2014 00:30 Go to previous message
sss111ind
Messages: 636
Registered: April 2012
Location: India
Senior Member

As My tables contain distinct set of rows your first query is working fine for me. Thank you very much for your help.
Previous Topic: SQL Query
Next Topic: How to Delete Parent table record
Goto Forum:
  


Current Time: Thu Aug 27 06:42:54 CDT 2026