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 December 2nd, 2004, 11:07 AM
sweetpea sweetpea is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 3 sweetpea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Join Help

I have three tables I need to get the data from.
Table a : General Info of a person
Table b: Addres of each person in table a (a person can have multiple addresses)
Table c: Mapping of zip code to county

I need to query the database such that, I get the columns from table a, columns of table b and county from table c. This is the query I use, I am getting multiple/duplicate rows.

Any comments ?

-----------------------------------------------------------
SELECT a.peopleID, a.FirstNAME, a.MiddleNAME, a.LastNAME, a.DOB, c.HouseNumber as HOUSE_NUMBER, c.Street as STREET, c.ApartmentNum as APARTMENT_NUMBER, c.CITY, c.STATE, c.Zip as ZIP_CODE
from TU_People a
INNER JOIN TU_Address c ON a.peopleID = c.peopleID
LEFT JOIN zipcodes zip on c.zip = zip.zipcode
where a.peopleID =1
-----------------------------------------------------------

Reply With Quote
  #2  
Old December 2nd, 2004, 11:16 AM
bocmaxima's Avatar
bocmaxima bocmaxima is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Aug 2004
Location: Tucson, Sonora
Posts: 1,322 bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 17 h 46 m 5 sec
Reputation Power: 23
Send a message via AIM to bocmaxima
You're joining the zipcode table but you're not using any fields from it in your query. Intentional? If so, then you can just exclude it and you'd probably solve your problem.

Otherwise, post your tables design (field names).
Also, have you tried changing the LEFT JOIN to an INNER JOIN?

Reply With Quote
  #3  
Old December 2nd, 2004, 12:24 PM
sweetpea sweetpea is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 3 sweetpea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Oh, I am sorry. Here is the corrected query.

Code:
SELECT a.peopleID, a.FirstNAME, a.MiddleNAME, a.LastNAME, a.DOB, c.HouseNumber as HOUSE_NUMBER, c.Street as STREET, c.ApartmentNum as APARTMENT_NUMBER, c.CITY, c.STATE, c.Zip as ZIP_CODE, zip.county as COUNTY  
from TU_People a
INNER JOIN TU_Address c ON a.peopleID = c.peopleID
LEFT JOIN zipcodes zip on c.zip = zip.zipcode
where a.peopleID =1




Quote:
Originally Posted by bocmaxima
You're joining the zipcode table but you're not using any fields from it in your query. Intentional? If so, then you can just exclude it and you'd probably solve your problem.

Otherwise, post your tables design (field names).
Also, have you tried changing the LEFT JOIN to an INNER JOIN?

Reply With Quote
  #4  
Old December 2nd, 2004, 12:26 PM
sweetpea sweetpea is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 3 sweetpea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes, and I tried changing it to INNER JOIN. Still does not work

Quote:
Originally Posted by sweetpea
Oh, I am sorry. Here is the corrected query.

Code:
SELECT a.peopleID, a.FirstNAME, a.MiddleNAME, a.LastNAME, a.DOB, c.HouseNumber as HOUSE_NUMBER, c.Street as STREET, c.ApartmentNum as APARTMENT_NUMBER, c.CITY, c.STATE, c.Zip as ZIP_CODE, zip.county as COUNTY  
from TU_People a
INNER JOIN TU_Address c ON a.peopleID = c.peopleID
LEFT JOIN zipcodes zip on c.zip = zip.zipcode
where a.peopleID =1

Reply With Quote
  #5  
Old December 2nd, 2004, 02:51 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,919 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 51 m 6 sec
Reputation Power: 1018
i doubt very much that you are getting duplicate rows, i.e. duplicate in all columns

your query is fine

there must be something wrong with the data

please show a few rows from each table that illustrate the problem you're having
__________________
r937.com | rudy.ca

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Join Help


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 3 hosted by Hostway
Stay green...Green IT