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 > Help with SQL query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-06-06, 11:13
jroof jroof is offline
Registered User
 
Join Date: Aug 2004
Posts: 2
Help with SQL query

I've got a query that I'd like help with. Here's the situation:
Table:
Person
Columns:
PersonID, Name, AddressID

Table:
Address
Columns:
AddressID, PersonID, Address, IsPreferred

A person can have one address or many. If a person has one address, the IsPreferred column is null. If a person has more than one address, one address will have a Y in IsPreffered, the remaining addresses will have a N.
If I write:
SELECT P.Name, A.AddressID, A.IsPreferred
FROM Person P
INNER JOIN Address A
ON P.AddressID = A.AddressID
I'll get:
Name AddressID IsPreferred
Bill 10 NULL
Ted 20 Y
Ted 30 N

What I want to do is to be able to return either the preferred address if there are multiple addresses or the single address with the null in the IsPreferred column so that:
Name AddressID IsPreferred
Bill 10 NULL
Ted 20 Y

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 09-06-06, 12:51
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool


Try this:
Code:
SELECT P.Name, A.AddressID, A.IsPreferred
  FROM Person P
 INNER JOIN Address A
    ON P.AddressID = A.AddressID
 WHERE NVL(IsPreferred,'Y') = 'Y';

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 09-06-06, 13:26
jroof jroof is offline
Registered User
 
Join Date: Aug 2004
Posts: 2
Thanks. It's a SQL Server box but replacing NVL with IsNull worked perfectly.
Reply With Quote
  #4 (permalink)  
Old 09-06-06, 14:27
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool


You are wellcome!
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #5 (permalink)  
Old 09-06-06, 15:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
replace it with COALESCE and you got a deal!!!

(this is the SQL forum, not the Oracle forum, not the SQL Server forum, so we should strive to use SQL, not some proprietary variant)

also, i think the join should be on PersonID
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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