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
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 1st, 2004, 09:31 AM
Dieter's Avatar
Dieter Dieter is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: SA, Centurion
Posts: 305 Dieter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 16 m 26 sec
Reputation Power: 6
How to compare 2 tables ??

Hi,

Need help with the structuring of the following query:
I've got 2 tables nl :tableA, tableB
I need to do a query to determine wat DISTINCT values of a column(cIdNo) in tableA is not present in column (bIdNo) in tableB.

tableA contains Id numbers of people who still has to pay their bills, and tableB contains Id numbers of people who already paid. So I want a query to determine who still needs to pay.

I've tried the following but doesn't work, not to sure if it's the right wayto do it:

PHP Code:
 SELECT  distinct(cIdno) as IdNumber 
FROM tableA 
LEFT JOIN tableB
ON tableA
.cIdNo != tableB.bIdNo 


PLEASE HELP !! Thanks in advance !!!
__________________
:P

Reply With Quote
  #2  
Old July 1st, 2004, 09:41 AM
Dieter's Avatar
Dieter Dieter is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: SA, Centurion
Posts: 305 Dieter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 16 m 26 sec
Reputation Power: 6
Sorry, been a long day... got it right eventually:

Here goes:
PHP Code:
 SELECT  distinct(cIdNo) as IdNumber 
FROM tableA 
WHERE tableA
.cIdNo not in (Select distinct(bIdNofrom  tableB


Any comments on the solution welcomed.

Reply With Quote
  #3  
Old July 1st, 2004, 11:09 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,775 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 16 h 24 m 58 sec
Reputation Power: 37
distinct is not a function. You dont need distinct when using in

Code:
SELECT  distinct cIdNo  as IdNumber
FROM tableA
WHERE cIdNo not in (Select bIdNo from  tableB) 

Reply With Quote
  #4  
Old July 2nd, 2004, 01:43 AM
Dieter's Avatar
Dieter Dieter is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: SA, Centurion
Posts: 305 Dieter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 16 m 26 sec
Reputation Power: 6
Thanks for your comments

I need to make use of distinct values in the first select because this is NOT a unique field, however doing a distinct select in the 2nd is irrelevant.

The final query:
PHP Code:
 SELECT DISTINCT(cLocationCode) as Code
FROM MedAidLocation
WHERE MedAidlocation
.cLocationCode
NOT IN
          
(SELECT pkc_LocationCode
     FROM FeeSchedule


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > How to compare 2 tables ??


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


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway