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
Solved

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

Posted on 2010-11-22
8
376 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query and VBA 5 45
SSRS Deployment problem 5 64
Where is the Help Section? 8 21
SQL Availablity Groups Shared Path 2 13
In this article I will describe the Backup & Restore 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

809 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