x

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

Say I have a table with three fields:

                    
PKID                    
UserID                    
PhoneNumber                    

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

avatar image

BrianLegg
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
show all comments (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 go create table t (pkid int, userid int, phonenumber varchar(10)) go insert t select 1, 1, '123' union all select 1, 1, '456' union all select 1, 1, '789'

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

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


--PIVOT for separate columns
select p.pkid
,p.userid
,[1] as phoneNumber1
,[2] as phoneNumber2
,[3] as phoneNumber3
from (
select pkid
,userid
,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

go

drop function dbo.fn_phoneCSV
drop table t

OUTPUT:
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

avatar image

TG
1.8k 3 5

(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'

select
pkid,userid,
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
from
(
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

avatar image

Madhivanan
1.1k 2 5 9

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

See also other questions here with the keyword "pivot"

http://ask.sqlteam.com/questions/tagged/pivot

more ▼

answered Nov 13, 2009 at 06:04 AM

avatar image

Kristen ♦
2.2k 7 11 14

(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.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x2016
x101

asked: Nov 12, 2009 at 04:57 PM

Seen: 4075 times

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

Copyright 2016 Redgate Software. Privacy Policy