| Author |
Topic |
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2008-12-17 : 00:58:20
|
| My result data is ID NOTES1 A1 B1 C2 D2 E2 FExpexted output is 1 A,B,C2 D,E,FAny funstion or ??? need to do in single query .. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-17 : 01:01:14
|
| [code]SELECT DISTINCT ID,STUFF((SELECT ','+ NOTES FROM Table WHERE ID=t.ID FOR XML PATH('')),1,1,'')FROM Table t[/code] |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-17 : 01:02:22
|
| SELECT distinct id,STUFF((select ',' + notes from urtable where id = t.id for xml path('')),1,1,' ')from urtable |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-17 : 01:04:18
|
quote: Originally posted by bklr SELECT distinct id,STUFF((select ',' + convert(varchar(12) ,notes) from urtable where id = t.id for xml path('')),1,1,' ')from urtable t
by a minutealso you've missed table alias t |
 |
|
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2008-12-17 : 01:08:53
|
| SELECT DISTINCT custid,STUFF((SELECT ','+ notes FROM q1 WHERE ID=q1.custid FOR XML PATH('')),1,1,'')FROM q1--drop table q1 create table q1 (custid int , notes varchar(2) )insert into q1 values(1,'a')insert into q1 values(1,'b')insert into q1 values(1,'c')insert into q1 values(2,'c')insert into q1 values(2,'d')insert into q1 values(2,'a')=============Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near 'XML'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-17 : 01:11:58
|
| are you using sql 2005? |
 |
|
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2008-12-17 : 01:15:21
|
| Sorry SQL 2000 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-17 : 01:21:39
|
quote: Originally posted by niranjankumark Sorry SQL 2000
then why you posted in sql 2005 forum?ok here's the sql 2000 solutioncreate a function as belowCREATE FUNCTION GetData(@ID int)RETURNS varchar(8000)ASDECLARE @List varchar(8000)SELECT @List=COALESCE(@List+ ',','') + NOTES FROM yourTableWHERE ID=@IDRETURN @ListGOthen use it like belowSELECT DISTINCT ID,dbo.GetData(ID)FROM YourTable |
 |
|
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2008-12-17 : 01:33:44
|
| Hi thanks .... is there any system defined funtion ?? because dont have rights to create... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-17 : 01:36:50
|
| nope. no system defined function. |
 |
|
|
|