Query that combines columns from multiple rows from source to one row in results.

Say I have a table with three fields:


There are three rows

Row1: 1 15 123                      
Row2: 1 15 456                     
Row3: 1 15 789                    

You can see that I have three separate phone numbers for userID 15. I want to display that in query results in one row: UserID, PhoneNumber1, PhoneNumber2, PhoneNumber3.

I have a much more complex query that gives me correct results in a cartesian product. I'm trying to eliminate the cartesian product.

more ▼

asked Nov 12, 2009 at 04:57 PM in Default

BrianLegg gravatar image

2 3 3 3

Do you want the phoneNumbers in seperate columns or a single column as a comma separated value?
Nov 12, 2009 at 05:22 PM TG
Seperate columns would be preferred. -BJL
Nov 12, 2009 at 05:49 PM BrianLegg
Ok, then see the PIVOT solution below:
Nov 12, 2009 at 05:59 PM TG
How many telephone numbers could there be, maximum, for a single person? That might pose a challenge if the number of columns it creates is high
Nov 13, 2009 at 06:03 AM Kristen ♦
one instance of 4 and many hundreds of 3,2, and 1. So, although there is no physical limit, practically, 5 is all I expect.
Nov 13, 2009 at 10:52 AM BrianLegg
(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

Here are alternatives for single column csv as well as pivot to separate columns:

use tempdb
create table t (pkid int, userid int, phonenumber varchar(10))
insert t
select 1, 1, '123' union all
select 1, 1, '456' union all
select 1, 1, '789'

create function dbo.fn_phoneCSV (@userid int)
returns varchar(50)
declare @out varchar(50)
select @out = coalesce(@out + ', ' + phoneNumber, phoneNumber)
from t
where userid = @userid
return @out

select pkid, userid, dbo.fn_phoneCSV(userid) as phoneNumbers
from t
group by pkid, userid

--PIVOT for separate columns
select p.pkid
,[1] as phoneNumber1
,[2] as phoneNumber2
,[3] as phoneNumber3
from (
select pkid
,phonenumber, row_Number() over (partition by userid order by phonenumber) as phSeq
from t
) d
pivot (max(phoneNumber) for phSeq in ([1],[2],[3])) p


drop function dbo.fn_phoneCSV
drop table t

pkid userid phoneNumbers
----------- ----------- --------------------------------------------------
1 1 123, 456, 789

pkid userid phoneNumber1 phoneNumber2 phoneNumber3
----------- ----------- ------------ ------------ ------------
1 1 123 456 789
more ▼

answered Nov 12, 2009 at 05:33 PM

TG gravatar image

1.8k 1 3

(comments are locked)
10|1200 characters needed characters left

or this

declare @t table (pkid int, userid int, phonenumber varchar(10)) 

insert @t
select 1, 1, '123' union all
select 1, 1, '456' union all
select 1, 1, '789'

max(case when sno=1 then phonenumber end) as phno1,
max(case when sno=2 then phonenumber end) as phno2,
max(case when sno=3 then phonenumber end) as phno3
select row_number() over(order by userid) as sno ,* from @t
) as t
group by pkid,userid
more ▼

answered Nov 13, 2009 at 03:52 AM

Madhivanan gravatar image

1.1k 1 3 6

(comments are locked)
10|1200 characters needed characters left

See also other questions here with the keyword "pivot"


more ▼

answered Nov 13, 2009 at 06:04 AM

Kristen gravatar image

Kristen ♦
2.2k 6 7 10

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Nov 12, 2009 at 04:57 PM

Seen: 3706 times

Last Updated: Nov 13, 2009 at 06:02 AM