Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6062343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:02:22+00:00 2026-05-23T09:02:22+00:00

Input ID RowID Data 1 1 S 1 1 Q 1 1 L 1

  • 0

Input

ID  RowID   Data
1   1   S
1   1   Q
1   1   L
1   1   Null
1   1   Null
1   1   S
1   1   E
1   1   R
1   1   V
1   1   E
1   1   R
1   1   Null
1   1   DB
1   2   S
1   2   T
1   2   A
1   2   C
1   2   K
2   1   O
2   1   V
2   1   E
2   1   R
2   1   Null
2   1   Null
2   1   Null
2   1   F
2   1   L
2   1   O
2   1   W

Expected Output

ID  NewData
1   SQL2SERVER1DB,STACK
2   OVER3FLOW
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:02:23+00:00Added an answer on May 23, 2026 at 9:02 am

    The only way to guarantee the order of the output is to add an column which defines the correct order to the table – I’ve used an IDENTITYcolumn.

    I’ve also assumed that there is a mistake in your sample data creation script (see my comment on the question) – the revised sample data creation script is as follows:

    DECLARE @t TABLE(ID INT IDENTITY(1,1) , groupId  INT, RowID INT, Data VARCHAR(10))
    INSERT INTO @t (groupId, RowID,DATA)
    SELECT 1,1,'S' UNION ALL SELECT 1,1,'Q' UNION ALL SELECT 1,1,'L' UNION ALL
    SELECT 1,1,NULL UNION ALL SELECT 1,1,NULL UNION ALL SELECT 1,1,'S' UNION ALL
    SELECT 1,1,'E' UNION ALL SELECT 1,1,'R' UNION ALL SELECT 1,1,'V' UNION ALL SELECT 1,1,'E' UNION ALL
    SELECT 1,1,'R' UNION ALL SELECT 1,1,NULL UNION ALL SELECT 1,1,'DB' UNION ALL
    SELECT 1,2,'S' UNION ALL SELECT 1,2,'T' UNION ALL SELECT 1,2,'A' UNION ALL SELECT 1,2,'C' UNION ALL
    SELECT 1,2,'K' UNION ALL SELECT 2,1,'O' UNION ALL SELECT 2,1,'V' UNION ALL SELECT 2,1,'E' UNION ALL 
    SELECT 2,1,'R'  UNION ALL SELECT 2,1,NULL UNION ALL SELECT 2,1,NULL UNION ALL SELECT 2,1,NULL
    UNION ALL SELECT 2,1,'F' UNION ALL SELECT 2,1,'L' UNION ALL SELECT 2,1,'O' UNION ALL SELECT 2,1,'W'
    

    It’s not possible, as far as I can see, to correctly aggregate the NULL rows and concatenate the strings in a single step, so this solution uses chained CTEs to carry out the steps one at a time. I’ve also used a CTE to deduplicate the output (rather than grouping).

    --convert consecutive nulls to a count
    ;WITH nullCTE
    AS
    (
        SELECT ID, ID AS e
        FROM @t AS t
        WHERE t.Data IS NULL
        AND EXISTS (SELECT 1 FROM @t AS q WHERE q.Data IS NOT NULL AND q.Id = t.ID + 1)
    
        UNION ALL
    
        SELECT  c.ID, t.Id
        FROM    @t AS t
        JOIN    nullCTE AS c
        ON      t.ID = c.e - 1
        WHERE t.Data IS NULL
    )
    -- simplify the null table
    ,grpCTE
    AS
    (SELECT ID, ID - e + 1 SIZE,
        ROW_NUMBER() OVER (PARTITION BY ID
                           ORDER BY e
                          ) AS rn
     FROM nullCTE
    )
    --build the output
    ,outputCTE
    AS
    (
        SELECT  t1.groupId,
                (
                    SELECT COALESCE(t.Data,CAST(g.size AS VARCHAR(11)),'') + CASE WHEN u.RowID > t.RowID AND u.groupId = t.groupId THEN ',' ELSE '' END
                    FROM        @t AS t
                    LEFT JOIN   grpCTE  AS g
                    ON          g.ID = t.ID
                    AND         rn = 1
                    LEFT JOIN   @t AS u
                    ON          u.ID = t.Id + 1
                    WHERE t.groupID = t1.groupId
                    ORDER BY t.Id
                    FOR XML PATH('') 
                ) AS NewData,
                ROW_NUMBER() OVER (PARTITION BY groupId
                                   ORDER BY Id
                                  ) AS rn
        FROM @t AS t1
    )
    SELECT groupId, NewData
    FROM outputCTE
    WHERE rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Input : {5, 13, 6, 5, 13, 7, 8, 6, 5} Output : {5,
Input: SHC 111U,SHB 22x,, SHA 5555G Needed output: SHB 22X, SHC 111U, SHA 5555G
Input: I have a LaTeX file, with plain text & math formulas. Desired output:
Input col1, col2, col3, coln Output @col1, @col2, @col3, @coln
Input XML: <Data> <BPP> <CP>A</CP> <Name>Joe</Name> </BPP> <BPP> <CP>A</CP> <Name>Show</Name> </BPP> <BPP> <CP>B</CP> <Name>Cunny</Name>
Hi I have an input as ID data 1 hello 2 sql The desired
I just would like to hear different opinions about ROWID type usage as input
how can i change cell input to readonly after i loaded all grid's data?
Input: AA1 The output will be: Char Count A 2 1 1
Input: # of seconds since January 1st, of Year 0001 Output: # of Full

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.