SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to aggregate the same data from one column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

duf
Starting Member

37 Posts

Posted - 06/06/2012 :  18:46:55  Show Profile  Reply with Quote
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

Edited by - duf on 06/06/2012 18:48:07

vijays3
Constraint Violating Yak Guru

India
311 Posts

Posted - 06/06/2012 :  19:17:53  Show Profile  Reply with Quote


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



Vijay is here to learn something from you guys.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000