Solved

MS SQL 2008 Query or view - multiple rows to single row help

Posted on 2010-11-22
8
363 Views
Last Modified: 2012-05-10
I have a database with several normalized tables.  In my customer table I have customer id, fname, lname.  In my emergency contact table I have customer id, contact phone.  I have anywhere from 1 to 10 emergency contact phone records for each customer ID.  I need to create a view or a query that will return records as one row.

I would like the first 3 distinct contact phone numbers to appear like this:

customer id, fname, lname, econtact#1, econtact#2, econtact#3

Obviously if they have 0, 1, or 2 emergency contact records then #3, #2, or #1 will may be blank since there may not be that many related emergency contact phone records.  But I have seen as many as 10 emergency contact records but I only want 3 - top 3 or first 3 returned will be fine.

THanks for your help.

Adam
0
Comment
Question by:afeind
  • 3
  • 3
  • 2
8 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_20923957.html

Use a UDF.  Pass in your customer id, returning your top 3 concatenated values.

then you can call it like:

select customerid, fname, lname, fn_getENumbers(customerid) from customers
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
Comment Utility
;with x as (
select a.id, a.fname, a.lname, b.phone,
rn=row_number() over (partition by a.id order by a.id)
from customer a left join contact b on a.id=b.customer_id)
select t1.id, t1.fname, t1.lname, t1.phone as phone1, t2.phone as phone2, t3.phone as phone3
from x t1
left join x t2 on t1.id=t2.id and t2.rn=2
left join x t3 on t1.id=t3.id and t3.rn=3
where t1.rn=1
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
Create Function fn_GetENumbers(customerid int)
returns varchar(100)
as
begin
DECLARE @Numbers varchar(100)

SELECT top3 @Numbers = COALESCE(@numbers + ', ', '') +
   CAST(contact_phone AS varchar(15))
FROM emergency_contact
WHERE customerid = @customerid

return @Numbers
end
0
 

Author Comment

by:afeind
Comment Utility
cyperkiwi - is there something missing from your example?

I am not sure I understand this solution.

I tried the function solution and could not get it to work.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
that query is as-is, assuming you have supplied the correct table and field names.
It should just be copy and paste
0
 

Author Comment

by:afeind
Comment Utility
Cyberkiwi - you are a true Genius - it works great.  One more question is there any way to display only distinct phone numbers?

For instance the emergency contact 1 may have the same number as emergency contact 2 - crazy I know but the customer will have emergency contact 1 as John Doe and emergency contact 2 as Jane Doe - both with the same number so rows may be like this
customer, number
1111, 555-1212
1111, 555-1212
1111, 555-1313
1111, 555-1313
1111, 555-1414
1111, 555-1818

would it be possible to return just first 3 distinct rows of phone numbers as t1, t2, t3?

Thanks!!!
0
 

Author Closing Comment

by:afeind
Comment Utility
Solution works flawlessly.  Thank you!
0
 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
This should do it.

;with x as (
select a.id, b.phone,
rn=row_number() over (partition by a.id order by a.id)
from customer a left join (select distinct top 3 id, phone from contact) b on a.id=b.id)
select t1.id, t1.phone as phone1, t2.phone as phone2, t3.phone as phone3
from x t1
left join x t2 on t1.id=t2.id and t2.rn=2
left join x t3 on t1.id=t3.id and t3.rn=3
where t1.rn=1

Regards
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now