MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old May 27th, 2004, 01:11 PM
inka inka is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 4 inka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

I have col1 and col2 in table1

Table1
col1 col2
1234 IM2
1234 M4
3456 IV5
6789 E98


Table2
col1 col2
1234 M325
3456 IV5
6789 E98

Record 1234 in table2 is missing IM2 and M4 values from table1. I need to write a query that will bring back records where table1.col1 = table2.col1 and table1.col2 not in table2.col2. I need to solve it as soon as possible. Please help. Thanks.

Reply With Quote
  #2  
Old May 28th, 2004, 12:36 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
give this a shot...
Code:
select	* 
from	Table1 T1
inner	join Table2 T2
   on	T1.col1 = T2.col1
where	T1.col2 not in
	(select	col2
	 from	Table2)

Reply With Quote
  #3  
Old May 28th, 2004, 11:46 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 18 m 16 sec
Reputation Power: 870
null, that won't work in all cases

what if there's a row in table2 with 3456 M4

then when you join the 1234 rows, 1234 M4 will fail because M4 is in the subquery, however, 1234 M4 isn't in table2
__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old May 29th, 2004, 10:36 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
[assuming the record you brought up in your reply exists in T2]

What do you mean when you say "1234 M4 will fail"? From what I took from the posters request, this record shouldn't return b/c the col2 value (M4) is in (select col2 from T2)...so this is excluded. Are you saying I should've responded w/something like this?...
Code:
select	* 
from	Table1 T1
inner	join Table2 T2
   on	T1.col1 = T2.col1 and T1.col2 <> T2.col2

Reply With Quote
  #5  
Old May 29th, 2004, 10:56 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 18 m 16 sec
Reputation Power: 870
if we have the following data

Table1
col1 col2
1234 IM2
1234 M4
3456 IV5
6789 E98


Table2
col1 col2
1234 M325
3456 M4
3456 IV5
6789 E98

then your subquery returns M325, M4, IV5, E98

but when you join table1's two 1234 rows to table2's single 1234 row, table1's IM2 isn't in the subquery results but M4 is

your revised query has problems too, since multiple matching rows in both tables will have a cross-join effect

Reply With Quote
  #6  
Old June 6th, 2004, 12:14 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
I'm confused...bummer Can you post your solution so I can study it?

Reply With Quote
  #7  
Old June 6th, 2004, 10:23 PM
brightlight brightlight is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 371 brightlight User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 32 m 27 sec
Reputation Power: 5
Hi Inka,

I'm not very sure whether the following query is what u expect but u can give a try to see the result.

Quote:

SELECT *
FROM Table1
UNION
SELECT *
FROM Table2
__________________
Regards,
Brightlight

Reply With Quote
  #8  
Old June 7th, 2004, 09:22 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 18 m 16 sec
Reputation Power: 870
Quote:
Originally Posted by Username=NULL
I'm confused...bummer Can you post your solution so I can study it?


simple left outer join

Code:
select Table1.col1
     , Table1.col2
  from Table1
left outer
  join Table2
    on Table1.col1
     = Table2.col1
   and Table1.col2  
     = Table2.col2
 where Table2.col2 is null           

Result: 
col1  col2
1234  IM2
1234  M4

Reply With Quote
  #9  
Old June 7th, 2004, 12:24 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Ah, but you see I take this...
Quote:
where table1.col1 = table2.col1 and table1.col2 not in table2.col2.

...as something totally different than...

where table1.col1 = table2.col1 and table1.col2 = table2.col2

....which is what your query is using. You're result produces the record...
Code:
col1   col2
1234   M4

...based off my interpretation, I disagree.

Reply With Quote
  #10  
Old June 7th, 2004, 01:54 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 18 m 16 sec
Reputation Power: 870
my result produces two rows, not just the one that you say it does

and you are right, it is totally based on interpretation

go back to the original post:

"Record 1234 in table2 is missing IM2 and M4 values from table1."

but if you look at the data with your interpretation, 1234 is also missing IV5 and E98

i'm pretty sure your interpretation is wrong

like the saying goes, "your mileage may vary; mine doesn't"


Reply With Quote
  #11  
Old June 7th, 2004, 05:26 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Let's use the tables you brought up (which is what I've been using all along)...
Code:
Table1
col1 col2
1234 IM2
1234 M4
3456 IV5
6789 E98

Table2
col1 col2
1234 M325
3456 M4 
3456 IV5
6789 E98

..and yes, your result does return 2 rows, I was just using that one though.
Code:
Result: 
col1  col2
1234  IM2
1234  M4

while 1234 is in table2, "m4" is in table2.col2, so I take that the record shouldn't return. I fully understand your solution though, just trying to show you where I was coming from, that's all. Good day Rudy.

Reply With Quote
  #12  
Old June 7th, 2004, 11:00 PM
brightlight brightlight is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 371 brightlight User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 32 m 27 sec
Reputation Power: 5
Hi Inka,

Just a suggestion to you. If you would have stated the required result in addition to the explanation (table1.col1 = table2.col1 and table1.col2 not in table2.col2...) it would have reduced the misinterpretations and saved the time.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.