How to combine several value in SQL


How can I combine serval lines' comments value if the line number more than 1? Need it in SQL statement.

Below is the current data output:

ID        Line     Type   Comment
23         1           A         This is
23         2           B          Test
24         1           C          Testing
25         1           B          Testing

Below is what I want to display:

ID        Line     Type   Comment
23         1           A         This is Test
23         2           B         This isTest
24         1           C          Testing
25         1           B          Testing

Thanks in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
Which database product?  The solution will likely be different.
slightwv (䄆 Netminder) Commented:
In Oracle as long as the comments aren't over 4000 characters, try this:
drop table tab1 purge;
create table tab1(id number, line number, type char(1), comments varchar2(10));

insert into tab1 values(23,1,'A','This is');
insert into tab1 values(23,2,'B','Test');
insert into tab1 values(24,1,'C','Testing');
insert into tab1 values(25,1,'B','Testing');

select id, line, type, listagg(comments, ' ') within group(order by line) over(partition by id) comments
from tab1

Open in new window

rowfeiAuthor Commented:
This is SQL server database. I also have several other tables to join together to come up different data fields.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

slightwv (䄆 Netminder) Commented:
>> I also have several other tables to join together to come up different data fields

I would add the additional information for the SQL Server Experts when they arrive.

The more accurate the information you provide, the more accurate the solution you will receive.
Jim HornSQL Server Data DudeCommented:
Agreed, we'll need an English sounding description of what you're trying to pull off here for us to provide a solution.

Guessing you'll want something like T-SQL:  Normalized data to a single comma delineated string and back but without the commas.
Arifhusen AnsariBusiness Intelligence Developer and AnalystCommented:
Hi rowfei,

You can try below code.


 (23,1,'A','This is') 

SELECT #Temp.ID,#Temp.LINE,#Temp.TYPE,


FROM #Temp T 
) AS Comment
FROM #Temp

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.