If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > How to compare value pair of one table with value pair of another table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-05-03, 17:16
s99shah s99shah is offline
Registered User
 
Join Date: Nov 2003
Posts: 1
Smile How to compare value pair of one table with value pair of another table

Here is the problem:
-----------------------
create table A( a1 int, a2 int)
create table B( b1 int, b2 int)

insert A values(1,1)
insert A values(1,2)
insert A values(2,1)
insert A values(2,3)
insert A values(3,1)
insert A values(3,3)

insert B values(1,1)
insert B values(1,2)
insert B values(3,2)
insert B values(2,2)

What is the SQL query to find out the pairs existing in B, but not in A ?

I can solve this by using string functions, but my problem is I have to write a query that can execute on MSSQL, DB2 and ORACLE, and as you know they have differnt syntax for string functions.

Thanks in advance.

Regards,
s99shah.
Reply With Quote
  #2 (permalink)  
Old 11-05-03, 20:48
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
pairs in B but not in A? left outer join, check for unmatched rows --
Code:
select b1, b2
  from B
left outer
  join A
    on b1 = a1
   and b2 = a2
 where a1 is null
rudy
http://r937.com/
Reply With Quote
  #3 (permalink)  
Old 11-12-03, 06:22
satish_ct satish_ct is offline
Registered User
 
Join Date: Nov 2003
Location: Bangalore, INDIA
Posts: 333
Thumbs up

Hi,

TRY THIS

Select B.* from a,b
where (+) a1 = b1;
__________________
SATHISH .
Reply With Quote
  #4 (permalink)  
Old 06-16-11, 13:34
fgm1477 fgm1477 is offline
Registered User
 
Join Date: Jun 2011
Posts: 3
More clean solution

select b1, b2
from b
where not exists
(
select 1
from a
where a1 = b1 and a2 = b2
)
Reply With Quote
  #5 (permalink)  
Old 06-16-11, 14:48
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
A third solution just for completeness - I guess rudy's join version is the most effective one (although Oracle will most probably generate the same execution plan for both queries)
Code:
select b1, b2
from b
where (b1, b2) not in (select a1, a2 from a);
Reply With Quote
  #6 (permalink)  
Old 06-16-11, 14:57
fgm1477 fgm1477 is offline
Registered User
 
Join Date: Jun 2011
Posts: 3
That's not Tsql

It's a mysql code very clean. Thanks
Reply With Quote
  #7 (permalink)  
Old 06-16-11, 15:35
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by fgm1477 View Post
That's not Tsql
you're right, it isn't

this is the ANSI SQL forum, not the Tsql forum

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 06-16-11, 16:02
fgm1477 fgm1477 is offline
Registered User
 
Join Date: Jun 2011
Posts: 3
I completly understood what you said. I think shammat posted a mysql code.


Thanks anyway for the clarification.
Reply With Quote
  #9 (permalink)  
Old 06-16-11, 16:11
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by fgm1477 View Post
I think shammat posted a mysql code.
um..... no

he posted ANSI SQL code
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 06-17-11, 04:43
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The query from satish_ct is not standard SQL. Everything else is/was.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On