Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
hztm2
Starting Member
16 Posts |
Posted - 2010-06-21 : 10:39:44
|
| Hi, I have comments in a table and I want to try and group these. EgJob Comment1 Comment12 Comment21 Comment3I want to query this so that I can combine the notes for Job 1 - so I get each note separated by a space or vbcrlf - so I get back something likeJob1 Comment1 Comment32 Comment2Thank you very much indeed for any assistance. |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-06-21 : 11:59:35
|
| Try this:DECLARE @Sample TABLE (JOB INT, COMMENT VARCHAR(500))INSERT @SampleSELECT 1, 'Comment1' UNION ALLSELECT 2, 'Comment2' UNION ALLSELECT 1, 'Comment3' -- Show the expected outputSELECT DISTINCT s1.JOB, STUFF((SELECT DISTINCT TOP 100 PERCENT + CHAR(10) + s2.COMMENT FROM @Sample AS s2 WHERE s2.JOB = s1.JOB ORDER BY + CHAR(10) + s2.COMMENT FOR XML PATH('')), 1, 1, '') AS COMMENTSFROM @Sample AS s1ORDER BY s1.JOBRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
daryljmorris
Starting Member
3 Posts |
Posted - 2010-06-21 : 13:41:32
|
| Why would you just not user a nested cursor???Daryl J MorrisMCPMCTS: SQL 2005 |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-21 : 14:49:27
|
| "Why would you just not user a nested cursor???"Cursors are very slow - would be a reason. |
 |
|
|
daryljmorris
Starting Member
3 Posts |
Posted - 2010-06-21 : 14:51:04
|
| use emisiiset nocount on Godeclare @job int, @comment char(20) Declare job_cu Cursor For Select Distinct job From job_comment Order By job Open job_cuFetch Next From job_cu Into @jobWhile @@FETCH_STATUS = 0 BeginPrint '***************'Print @jobPrint '_______________'Declare comment_cu Cursor For Select Distinct comment From job_comment Where job = @job Order By comment Open comment_cuFetch Next From comment_cu Into @commentWhile @@FETCH_STATUS = 0Begin Print @comment Fetch Next From comment_cu Into @commentEnd Close comment_cu Deallocate comment_cu Fetch Next From job_cu Into @jobEndClose job_cuDeallocate job_cuGet the Results of ***************1_______________comment 1 comment 2 ***************2_______________comment 1 comment 2 Daryl J MorrisMCPMCTS: SQL 2005 |
 |
|
|
daryljmorris
Starting Member
3 Posts |
Posted - 2010-06-21 : 14:54:23
|
| Try this:DECLARE @Sample TABLE (JOB INT, COMMENT VARCHAR(500))INSERT @SampleSELECT 1, 'Comment1' UNION ALLSELECT 2, 'Comment2' UNION ALLSELECT 1, 'Comment3'-- Show the expected outputSELECT DISTINCT s1.JOB,STUFF((SELECT DISTINCT TOP 100 PERCENT + CHAR(10) + s2.COMMENT FROM @Sample AS s2 WHERE s2.JOB = s1.JOB ORDER BY + CHAR(10) + s2.COMMENT FOR XML PATH('')), 1, 1, '') AS COMMENTSFROM @Sample AS s1ORDER BY s1.JOBI have never used that though and it works pretty good. This is why I signed up on here lol.Daryl J MorrisMCPMCTS: SQL 2005 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-22 : 04:51:02
|
quote: Originally posted by hztm2 Hi, I have comments in a table and I want to try and group these. EgJob Comment1 Comment12 Comment21 Comment3I want to query this so that I can combine the notes for Job 1 - so I get each note separated by a space or vbcrlf - so I get back something likeJob1 Comment1 Comment32 Comment2Thank you very much indeed for any assistance.
This is known as Suppress if duplicated featureDo this in your reporting toolMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-06-22 : 04:56:23
|
quote: Originally posted by daryljmorris Why would you just not user a nested cursor???Daryl J MorrisMCPMCTS: SQL 2005
Why would you just not use a reporting tool???MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|