October 4, 2012 at 7:19 am
Hello All
Can someone help me with the following:
I need to take the duplicate rows and merge the telephone data onto the original record as additional columns.
drop table temp
create table temp(
nameid nvarchar(10),
telephone nvarchar(12))
insert into temp
values ('1','01234567890'),('1','01234567899'),('2','02345678998'),('2','02345678999'),('2','02345679000')
The final table needs to look like
1,01234567890,01234567899
2,02345678998,02345678999,02345679000
Thanks
October 4, 2012 at 7:43 am
bicky1980 (10/4/2012)
Hello AllCan someone help me with the following:
I need to take the duplicate rows and merge the telephone data onto the original record as additional columns.
drop table temp
create table temp(
nameid nvarchar(10),
telephone nvarchar(12))
insert into temp
values ('1','01234567890'),('1','01234567899'),('2','02345678998'),('2','02345678999'),('2','02345679000')
The final table needs to look like
1,01234567890,01234567899
2,02345678998,02345678999,02345679000
Thanks
Strictly speaking it's impossible in RDBMS.
Number of columns is the same for all rows in a table.
So, you cannot have one row with 3 columns and another one with 4;-)
What you could do instead, you may have all rows to have the same number of columns, but contain empty/null values in some of them. In a case, final table could look something like:
1,01234567890,01234567899
2,02345678998,02345678999,02345679000
You can do it with Dynamic Cross-Tab query (search for example on this forum and you will find very good one :-)).
Saying that you have not specified the "final" table DDL. So, from what you have posted someone may thing that you want just one column with comma separated values in it. If so, you can do this:
select cast(nameid as varchar)+ (select ',' + telephone from #temp t2
where t2.nameid = t1.nameid
for xml path(''))
from #temp t1
October 4, 2012 at 8:03 am
Sorry, I wasn't very clear.
The final table would have empty columns where there was less telephone numbers.
I want each telephone number in a sperate column on the record e.g. telno1, telno2, telno3
Thanks
October 4, 2012 at 8:08 am
Do you have another column in your existing table to identify what type of phone number it is? Mobile/work/home etc?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 4, 2012 at 8:31 am
No they are all labelled telno
October 4, 2012 at 8:36 am
SELECT
nameid,
telephone1 = MAX(CASE WHEN rn = 1 THEN telephone END),
telephone2 = MAX(CASE WHEN rn = 2 THEN telephone END),
telephone3 = MAX(CASE WHEN rn = 3 THEN telephone END),
telephone4 = MAX(CASE WHEN rn = 4 THEN telephone END)
FROM (SELECT *, rn = ROW_NUMBER() OVER(PARTITION BY nameid ORDER BY telephone) FROM temp) d
GROUP BY nameid
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 4, 2012 at 10:24 am
Fantastic! Works Great!
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply