Solved

select one record of several

Posted on 2011-09-06
3
188 Views
Last Modified: 2012-06-27
I have a table of phone numbers with the following structure:

PersonID, PhoneType, PhoneNumber

Each person can have several phone numbers with different types, and not everybody has the same type.

I am looking for a view which gives me just one phone number, selected by a pre-established order of phonetype. Whenever the view has found this type, it should continue to the next person.

Example:
1000,HOME,1231231110
1000,WORK,1231231111
1000,CELL,1231231112
1001,WORK,1231231113
1002,CELL,1231231114
1002,MESS,1231231115
1003,FAM,1231231116
1004,HOME,1231231117
1004,FAM,1231231118

my selected order is: HOME - WORK - CELL

then the view should contain:

1000,1231231110
1001,1231231113
1002,1231231114
1004,1231231117

How would I write this select statement?

Thanks!
0
Comment
Question by:DerCornet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
markterry earned 500 total points
ID: 36492684
Try this? may not be the prettiest solution, but if you want a view, that should work. I would probably do something with CTE and a procedure if I didnt know what phone types to expect though.
Select 
PersonID, PhoneType, PhoneNumber
from PhoneNumbers
Where PhoneType = 'Home' 
UNION ALL
Select 
PersonID, PhoneType, PhoneNumber
from PhoneNumbers
Where PhoneType = 'Work' 
and PersonID not in
(Select PersonID rom PhoneNumbers Where PhoneType IN ('Home'))
UNION ALL
Select 
PersonID, PhoneType, PhoneNumber
from PhoneNumbers
Where PhoneType = 'Cell'
and PersonID not in
(Select PersonID rom PhoneNumbers Where PhoneType IN ('Home', 'Work'))

Open in new window

0
 
LVL 42

Expert Comment

by:dqmq
ID: 36492689
Select PersonID, PhoneType, PhoneNumber
From
(
Select *, row_number() over(partition by PersonID
 order by  
 case when PhoneType = 'Home' then 1
         when PhoneType = 'Work' then 2
         when PhoneType = 'Cell' then 3
                                               else   4
         end asc
        ) as precedence
 from YourTable) as T1
where T1.precedence = 1

   
0
 

Author Closing Comment

by:DerCornet
ID: 36499429
The solution works fine, but you are right, it doesn't seem to be an elegant solution. I leave the elegant form for another day. I needed a solution now, and that's what it does. (It became a little more involved as I have to look for 7 different phone types ...) And I fear it is a rather slow solution.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question