Solved

Concatenate 5 rows at a time

Posted on 2014-02-20
3
201 Views
Last Modified: 2014-02-20
How can I Concatenate 5 rows at a time.

ID
518
519
520
521
522
523
524
525
526
527

Desired results...

518,519,520,521,522
523,524,525,526,527
0
Comment
Question by:JRockFL
3 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
Comment Utility
CREATE TABLE #Test
(
      ID      INT
)

INSERT INTO #Test (ID)
VALUES (518),
    (519),
      (520),
      (521),
      (522),
      (523),
      (524),
      (525),
      (526),
      (527)

WITH cte
AS
(
      SELECT ROW_NUMBER() OVER(ORDER BY ID) AS RowNumber,
            (ROW_NUMBER() OVER(ORDER BY ID) - 1) / 5 AS GroupNumber,
            ID
      FROM #Test
)
SELECT DISTINCT GroupNumber,
      STUFF(
      (
            SELECT ',' + CAST(ID AS VARCHAR(20))
            FROM cte
            WHERE GroupNumber = cte1.GroupNumber
            FOR XML PATH('')
      ), 1, 1, '') AS IDList
FROM cte AS cte1
0
 
LVL 8

Author Comment

by:JRockFL
Comment Utility
perfect!
i would have never figured that out.
thank you
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Very similar; not aware of the other, of course, as I was writing this :-) :


IF OBJECT_ID('tempdb.dbo.#IDs') IS NOT NULL
    DROP TABLE #IDs
CREATE TABLE #IDs (
    ID int,
    row_group int,
    PRIMARY KEY ( row_group, ID )
    )
INSERT INTO #IDs
SELECT ID, (ROW_NUMBER() OVER (ORDER BY ID) - 1) / 5 AS row_group
FROM dbo.tablename

;WITH
cteDigits AS (
    SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
    SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
),
cteTally AS (
    SELECT [1000000s].digit * 1000000 + [100000s].digit * 100000 + [10000s].digit * 10000 +
        [1000s].digit * 1000 + [100s].digit * 100 + [10s].digit * 10 + [1s].digit AS tally
    FROM cteDigits [1s]
    CROSS JOIN cteDigits [10s]
    CROSS JOIN cteDigits [100s]
    CROSS JOIN cteDigits [1000s]    
    CROSS JOIN cteDigits [10000s]
    CROSS JOIN cteDigits [100000s]
    CROSS JOIN cteDigits [1000000s]
)
SELECT STUFF((
    SELECT ',' + CAST(ID AS varchar(10))
    FROM #IDs i
    WHERE
        i.row_group = c.tally
    ORDER BY ID
    FOR XML PATH('')
    ), 1, 1, '')
FROM cteTally c
WHERE
    c.tally <= (SELECT COUNT(*) / 5 FROM #IDs)
ORDER BY
    c.tally
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now