Sorry about a vague title, but here's what i am trying to do.
I have the following two tables
TABLE_PERSON | TABLE_PHONE | col_Name | col_Name col_phoneID col_phoneNbr -------- | -------- ----------- ------------ Clark Kent | Clark Kent 1 111-111-1111 Bruce Wayne | Clark Kent 2 222-222-2222 Peter Parker | Peter Parker 2 333-333-3333 | Peter Parker 3 444-444-4444 | Bruce Wayne 3 555-555-5555 | Bruce Wayne 4 666-666-6666
The col_Name is actually an ID field, but i am using a name to demostrate. Here's what i want to get by using one SQL statement
col_Name col_phoneNbr -------- ------------ Clark Kent 111-111-1111 Peter Parker 333-333-3333 Bruce Wayne 555-555-5555
The phone_ID is actually a type of phone number such as main, fax, toll-free, etc. So in my output i would like to have a phone number for each individual, but be selected in a such a way that if type "1" is not available, select type "2", if type "2" is not there, select type "3", etc. In my case there are 6 types, and their priority is something like 4 > 2 > 1 > 3 > 5 > 6, so i would like to be able to select only the first available number, and NULL if neither is there.
I did this in a previous project by using a very convoluted method where i would select all type "1" and then all type "2" while filtering type "1" using a "NOT IN" clause and then UNIONing the two sets together. Actually, it ended up being like 4 sets, and for the 1000+ records i was pulling out, it ran really slow.
I should mention that this is MS SQL Server 2000, so i can't use any 2005 features.
Thanks in advance!