Solved

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

Posted on 2010-11-22
8
374 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 40

Expert Comment

by:Kyle Abrahams
ID: 34192057
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
ID: 34192069
;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 40

Expert Comment

by:Kyle Abrahams
ID: 34192085
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
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.

 

Author Comment

by:afeind
ID: 34193597
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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34193599
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
ID: 34193807
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
ID: 34193813
Solution works flawlessly.  Thank you!
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34199495
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

808 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