Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
i have a table tb_product has two columns product and part no

product-----------part_no
1------------------ a
1------------------ b
1------------------ c
1------------------ d
1------------------ e
2------------------ a
2------------------ b
2------------------ c
3------------------ a
3------------------ b
3------------------ c

i wanna output like that
product----------- part no
1------------------ a,b,c,d,e
2----------------- a,b,c
3----------------- a,b,c

please write a query for this output.......

thanks & regards
Srishti
Posted

SQL
select distinct product,stuff((select ',' + part_no from TblNm t
where t.product=a.product FOR XML PATH('')),1,1,'') as a
from TblNm as a

Happy Coding!
:)
 
Share this answer
 
v2
Comments
srishti_ 5-Apr-13 1:51am    
This query show an error that.... "The multi-part identifier "a.product" could not be bound."
Aarti Meswania 5-Apr-13 1:54am    
it's ...
select distinct product,stuff((select ',' + part_no from TblNm t
where t.product=a.product FOR XML PATH('')),1,1,'') as Part_No
from TblNm as a
srishti_ 5-Apr-13 2:00am    
now ok thanks
Aarti Meswania 5-Apr-13 2:15am    
Welcome!
Glad to help you!:)
Try this:
SQL
SELECT  t1.[product], STUFF( (SELECT ', ' + [part_no] AS 'text()' 
                  FROM YourTable AS t2
                  WHERE t2.product = t1.product
                  FOR XML PATH('')), 1, 1, '') AS [Keywords]
FROM YourTable AS t1
GROUP BY t1.[product]
 
Share this answer
 
Comments
srishti_ 5-Apr-13 1:54am    
thanks
Maciej Los 5-Apr-13 1:55am    
You're welcome ;)
Sandeep Mewara 5-Apr-13 1:59am    
For this once I am not voting Mac.

It's clearly a homework without any effort whatsoever. Another link to same question by another member: http://www.codeproject.com/Questions/572798/NeedplusaplusQueryplusforplusbelow

Looks like homework of the day!
Maciej Los 5-Apr-13 2:01am    
OK ;)
Maciej Los 5-Apr-13 3:02am    
2 stars for worknig example??? I don't get it.
It does not work like this here.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.
 
Share this answer
 
Comments
RedDk 5-Apr-13 12:26pm    
And THIS is a rant ...
Sandeep Mewara 5-Apr-13 13:17pm    
Well, this is an advice. Now it depends on you if you take it otherwise.

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