Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to aggregate the same data from one column

Author  Topic 

duf
Starting Member

39 Posts

Posted - 2012-06-06 : 18:46:55
I have loaded .csv file into a table with three columns. In one of the columns are repeated exactly the same data:

ID CN OPIS
-------------
1 123 AAA
2 000 eee
3 123 BBB
4 555 ccc

Is it possible to combine items 1 and 3 so as to have a one record of the aggregate of:

ID CN OPIS
-------------
1 123 AAA,BBB
2 000 eee

4 555 ccc

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-06-06 : 19:17:53
[code]

create table tab(id int ,cn int ,OPIS varchar(20))

insert into tab(id,cn,OPIS)
select 1,123,'AAA'
union all
select 2, 000,'eee'
union all

select 3,123,'BBB'
union all

select 4,555,'ccc'




create function fn (@cn int)

returns varchar(max)as
begin

declare @str varchar(200)

select @str = coalesce(@str+',','')+ opis from tab where cn = @cn

return @str

end

select distinct cn ,dbo.fn(cn) as opis from tab

[/code]

Vijay is here to learn something from you guys.
Go to Top of Page
   

- Advertisement -