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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multi Rows as group

Author  Topic 

learntsql

524 Posts

Posted - 2010-01-25 : 07:41:51
Hi..
DECLARE @Sample TABLE (ID INT, Code VARCHAR(50))

INSERT @Sample
SELECT 1, 'One' UNION ALL
SELECT 2, 'TWO' UNION ALL
SELECT 3, 'THREE' UNION ALL
SELECT 4, 'FOUR' union all
SELECT 5, 'Five' UNION ALL
SELECT 6, 'SIX' UNION ALL
SELECT 7, 'SEVEN' UNION ALL
SELECT 8, 'EIGHT' UNION ALL
SELECT 9, 'NINE' UNION ALL
SELECT 10, 'TEN'

My output should be as
ID-Values
1-One,Two,Three,Four
2-Five,Six,Seven,Eight
3-Nine,Ten

If My Sample table contains Only 5 rows then it should show
One to Four in first line
and remaining items in second line
TIA.

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+3
WHERE S1.ID % 4 = 1
[/code]
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-25 : 07:55:50
Sorry, missed the ID off:

SELECT (S1.ID/4)+1 AS ID, ...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-25 : 07:56:35
if sql 2005 use
SELECT 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

Go to Top of Page

learntsql

524 Posts

Posted - 2010-01-25 : 07:58:21
Thnaks Kristen,Visakh
Perfect...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-25 : 08:00:18
welcome
Go to Top of Page

learntsql

524 Posts

Posted - 2010-01-25 : 08:10:00
Sorry guys bit more requirement
DECLARE @Sample TABLE (ID INT, CODE VARCHAR(50))

INSERT @Sample
SELECT 1, 'One' UNION ALL
SELECT 2, 'TWO' UNION ALL
SELECT 4, 'FOUR' union all
SELECT 5, 'Five' UNION ALL
SELECT 6, 'Six'

In this Case it should display as
ID-Values
1-One,Two,Four,Five
2-Six
The rule is Four values/records in single row
Go to Top of Page

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 requirement
DECLARE @Sample TABLE (ID INT, CODE VARCHAR(50))

INSERT @Sample
SELECT 1, 'One' UNION ALL
SELECT 2, 'TWO' UNION ALL
SELECT 4, 'FOUR' union all
SELECT 5, 'Five' UNION ALL
SELECT 6, 'Six'

In this Case it should display as
ID-Values
1-One,Two,Four,Five
2-Six
The rule is Four values/records in single row


are you using sql 2005?
Go to Top of Page

learntsql

524 Posts

Posted - 2010-01-25 : 08:13:57
yes
Go to Top of Page

learntsql

524 Posts

Posted - 2010-01-25 : 08:34:37
Any clue plz...
Go to Top of Page

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

learntsql

524 Posts

Posted - 2010-01-25 : 08:58:38
This is for part of Report display
Go to Top of Page

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,Code
FROM 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]
Go to Top of Page

learntsql

524 Posts

Posted - 2010-01-27 : 00:04:46
Thank you very much gurus
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-27 : 11:43:02
welcome
Go to Top of Page
   

- Advertisement -