Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-22
8
Medium Priority
?
381 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
[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
  • 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 2000 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

704 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