Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have my table data when writing the select query in the below format

1 jks abc
1 kjs bcde
2 hjd rty

now how can i get the data

1 jks abc,bcde
1 kjs abc,bcde
2 hgd rty


Thanks in advance
Posted
Comments
DaveAuld 20-Sep-12 2:16am    
That doesn't make much sense? the second one, just has an extra column of data, but doesn't really demonstrate where the data came from, maybe need to edit and redifine your example.

1 solution

see below example
Input Table                 Output Table
------------                --------------
a       b                   a     b
------------                --------
1	a                   1	  a,b
1	b	------>     2	  c
2	c        Query 

SQL
select a,substring(b,0,Len(b)) as b from
(
	select distinct a,
	(
		select b + ',' as [text()] from 
		(
			select 1 as a,'a' as b
			union all
			select 1 as a,'b' as b
			union all
			select 2 as a,'c' as b
		) as tbl_a where tbl_a.a=tbl_b.a
		for xml path('')	
	) as b

	from
        (
		select 1 as a,'a' as b
		union all
		select 1 as a,'b' as b
		union all
		select 2 as a,'c' as b
	) as tbl_b
)
as tbl_c

follow steps given below for modifying above query as per your table structure
replace underline section with your table-name
a=your columnname e.g. id
b=column name which you want to combine with ','

another example
how to get a single row from a table from multiple rows[^]

Happy Coding!
:)
 
Share this answer
 
v4
Comments
Kuthuparakkal 20-Sep-12 2:47am    
nice stuff, my 5+
Aarti Meswania 20-Sep-12 2:48am    
Thank you!
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900