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 |
|
learntsql
524 Posts |
Posted - 2010-01-25 : 07:41:51
|
| Hi..DECLARE @Sample TABLE (ID INT, Code VARCHAR(50))INSERT @SampleSELECT 1, 'One' UNION ALLSELECT 2, 'TWO' UNION ALLSELECT 3, 'THREE' UNION ALLSELECT 4, 'FOUR' union allSELECT 5, 'Five' UNION ALLSELECT 6, 'SIX' UNION ALLSELECT 7, 'SEVEN' UNION ALLSELECT 8, 'EIGHT' UNION ALLSELECT 9, 'NINE' UNION ALLSELECT 10, 'TEN' My output should be asID-Values1-One,Two,Three,Four2-Five,Six,Seven,Eight3-Nine,TenIf My Sample table contains Only 5 rows then it should showOne to Four in first lineand remaining items in second lineTIA. |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-25 : 07:54:12
|
| [code]SELECT S1.Code + COALESCE(', ' + S2.Code, '') + COALESCE(', ' + S3.Code, '') + COALESCE(', ' + S4.Code, '')FROM @Sample AS S1 LEFT OUTER JOIN @Sample AS S2 ON S2.ID = S1.ID+1 LEFT OUTER JOIN @Sample AS S3 ON S3.ID = S1.ID+2 LEFT OUTER JOIN @Sample AS S4 ON S4.ID = S1.ID+3WHERE S1.ID % 4 = 1[/code] |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-25 : 07:55:50
|
Sorry, missed the ID off:SELECT (S1.ID/4)+1 AS ID, ... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-25 : 07:56:35
|
if sql 2005 useSELECT Seq,STUFF((SELECT ',' + Code FROM table where ((ID-1)/4)+1=t.Seq FOR XML PATH('')),1,1,'') AS [Values]FROM(SELECT DISTINCT ((ID-1)/4)+1 AS Seq FROM table)t |
 |
|
|
learntsql
524 Posts |
Posted - 2010-01-25 : 07:58:21
|
| Thnaks Kristen,VisakhPerfect... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-25 : 08:00:18
|
welcome |
 |
|
|
learntsql
524 Posts |
Posted - 2010-01-25 : 08:10:00
|
| Sorry guys bit more requirementDECLARE @Sample TABLE (ID INT, CODE VARCHAR(50))INSERT @SampleSELECT 1, 'One' UNION ALLSELECT 2, 'TWO' UNION ALLSELECT 4, 'FOUR' union allSELECT 5, 'Five' UNION ALLSELECT 6, 'Six' In this Case it should display asID-Values1-One,Two,Four,Five2-SixThe rule is Four values/records in single row |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-25 : 08:12:13
|
quote: Originally posted by learntsql Sorry guys bit more requirementDECLARE @Sample TABLE (ID INT, CODE VARCHAR(50))INSERT @SampleSELECT 1, 'One' UNION ALLSELECT 2, 'TWO' UNION ALLSELECT 4, 'FOUR' union allSELECT 5, 'Five' UNION ALLSELECT 6, 'Six' In this Case it should display asID-Values1-One,Two,Four,Five2-SixThe rule is Four values/records in single row
are you using sql 2005? |
 |
|
|
learntsql
524 Posts |
Posted - 2010-01-25 : 08:13:57
|
| yes |
 |
|
|
learntsql
524 Posts |
Posted - 2010-01-25 : 08:34:37
|
| Any clue plz... |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-01-25 : 08:56:44
|
| do it in your presentation layer. Much, much easier to implement a record count and concat there.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
learntsql
524 Posts |
Posted - 2010-01-25 : 08:58:38
|
| This is for part of Report display |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-25 : 09:38:36
|
| [code];With CTE(Seq,ID,Code) AS(SELECT ROW_NUMBER() OVER (ORDER BY ID),ID,CodeFROM Table)SELECT ID,STUFF((SELECT ',' + Code FROM CTE where ((Seq-1)/4)+1=t.ID FOR XML PATH('')),1,1,'') AS [Values]FROM(SELECT DISTINCT ((Seq-1)/4)+1 AS ID FROM CTE)t[/code] |
 |
|
|
learntsql
524 Posts |
Posted - 2010-01-27 : 00:04:46
|
| Thank you very much gurus |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-27 : 11:43:02
|
welcome |
 |
|
|
|
|
|
|
|