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 > Select one if active else another

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-18-05, 16:55
DKolls DKolls is offline
Registered User
 
Join Date: Sep 2004
Posts: 2
Select one if active else another

I have a database of address info:
IDNUM ADDTYPE SEQNUM STATUS STREET1 etc
1 RS 1 123 Main
1 BU 2 456 Main
2 RS 1 I 789 Main
2 BU 2 987 Elm
and so on

Some have have active RS (residence) and BU (business) addresses,
some have only one or the other active.

I need to select only one active address per IDNUM, If they have an active RS I need that one, if not then I can use the active BU.

How can I select these without duplicating the records with both types active?
Reply With Quote
  #2 (permalink)  
Old 02-18-05, 17:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
assume STATUS='I' means inactive (this was my best guess, based on your information)
Code:
select ID
     , ADDTYPE
     , SEQNUM 
     , STATUS 
     , STREET1
  from yourtable as o
 where STATUS <> 'I'   /* check this */
   and ADDTYPE =
     ( select max(ADDTYPE)
         from yourtable
        where ID = o.ID
          and STATUS = o.STATUS )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-18-05, 17:31
DKolls DKolls is offline
Registered User
 
Join Date: Sep 2004
Posts: 2
Thank you

That will do the trick.

Thank you very much.

I forget about using the max and min with anything but numbers.
Reply With Quote
  #4 (permalink)  
Old 02-23-05, 05:51
jay82 jay82 is offline
Registered User
 
Join Date: Feb 2005
Location: London
Posts: 19
Faster Version

How about this ...?

SELECT o.*
FROM ADD_INFO AS o
INNER JOIN ADD_INFO AS i
ON o.IDNUM = i.IDNUM
AND o.STATUS = i.STATUS
AND o.ADDTYPE > i.ADDTYPE

I think this will be faster if there are more records...

What do you think r937??
Reply With Quote
  #5 (permalink)  
Old 02-23-05, 05:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
jay82, you are not testing for active status

plus, i think you will still need an outer join

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 02-23-05, 08:09
jay82 jay82 is offline
Registered User
 
Join Date: Feb 2005
Location: London
Posts: 19
You are right ... I don't think it's possible with even LEFT OUTER JOIN... but i am still trying...
Reply With Quote
  #7 (permalink)  
Old 02-23-05, 09:01
jay82 jay82 is offline
Registered User
 
Join Date: Feb 2005
Location: London
Posts: 19
Any problem with this query??

SELECT o.*, i.*
FROM ADD_INFO AS o
LEFT OUTER JOIN ADD_INFO AS i
ON o.IDNUM = i.IDNUM
AND o.ADDTYPE <> i.ADDTYPE
WHERE i.ADDTYPE IS NULL
OR o.ADDTYPE > i.ADDTYPE
AND o.STATUS <> 'I'
Reply With Quote
  #8 (permalink)  
Old 02-23-05, 09:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, a major problem

your ANDs and ORs need parentheses

plus, i don't think the status is being handled correctly

are you guessing, or testing?

because the best way to develop queries is to test them
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 02-24-05, 06:19
jay82 jay82 is offline
Registered User
 
Join Date: Feb 2005
Location: London
Posts: 19
Here's your parentheses ... and it's tested... but still if you are getting different result let me know... it will be good thing to learn... that's why we are here .. !

SELECT o.*, i.*
FROM ADD_INFO AS o
LEFT OUTER JOIN ADD_INFO AS i
ON o.IDNUM = i.IDNUM
AND o.ADDTYPE <> i.ADDTYPE
WHERE (i.ADDTYPE IS NULL
OR o.ADDTYPE > i.ADDTYPE)
AND o.STATUS <> 'I'
Reply With Quote
  #10 (permalink)  
Old 02-24-05, 06:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
what about "If they have an active RS I need that one, if not then I can use the active BU."

you seem to be returning both of them

and you're not testing i.STATUS either

__________________
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