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

  • Home
  • SEARCH
  • 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 8200563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:29:31+00:00 2026-06-07T06:29:31+00:00

I have a table of data: |Key|Field |DateField | |A |Value1|2012-06-01| |A |Value2|2012-06-02| |A

  • 0

I have a table of data:

|Key|Field |DateField |
|A  |Value1|2012-06-01|
|A  |Value2|2012-06-02|
|A  |Value3|2012-06-03|
|B  |Value4|2012-06-04|
|B  |Value5|2012-06-05|

I want to aggregate this string data by key – like in a group but I can’t work out how.
What I want to end up with is:

|Key|Field Aggregate       |
|A  |Value1, Value2, Value3|
|B  |Value4, Value5        |

Better still would be to end up with the sequence position inserted into the aggregation where the order of the sequence is determined by the order of the corresponding date field.

|Key|Field Aggregate                |
|A  |1: Value1, 2: Value2, 3: Value3|
|B  |1: Value4, 2: Value5           |

This seems like something I could do with a Common Table Expression, but I don’t seem to be able to work out how.

I wouldn’t ordinarily perform this kind of operation in SQL – I’d normally pull the data into an app and manipulate it as necessary in code.

Unfortunately this does not appear to be an option in this instance.

How can I do this in SQL Server 2005 T-SQL.

Is a CTE the right approach? Is there something more appropriate?

  • 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-06-07T06:29:32+00:00Added an answer on June 7, 2026 at 6:29 am

    Given this sample data:

    CREATE TABLE #t
    (
      [Key]     char(1),
      [Field]   varchar(32), 
      DateField datetime
    );
    
    INSERT #t([Key],[Field],DateField) 
      SELECT 'A','Value1','20120601'
      UNION ALL SELECT 'A','Value2','20120602'
      UNION ALL SELECT 'A','Value3','20120603'
      UNION ALL SELECT 'B','Value4','20120604'
      UNION ALL SELECT 'B','Value5','20120605';
    

    In versions of SQL Server older than SQL Server 2017, we can do this:

    SELECT [Key], [Field Aggregate] = STUFF(
      (SELECT ', ' + CONVERT(varchar(11), 
          ROW_NUMBER() OVER (PARTITION BY [Key] ORDER BY DateField)) 
          + ': ' + [Field]
        FROM #t AS t2 WHERE t2.[Key] = t.[Key]
        ORDER BY DateField
        FOR XML PATH(''), 
        TYPE).value(N'./text()[1]', N'varchar(max)'), 1, 2, '')
      FROM #t AS t
      GROUP BY [Key] ORDER BY [Key];
    

    Results:

    Key Field Aggregate
    A 1: Value1, 2: Value2, 3: Value3
    B 1: Value4, 2: Value5
    • Example db<>fiddle

    In SQL Server 2017 or greater, we have a much easier time using STRING_AGG():

    ;WITH cte AS
    (
      SELECT [Key], [Field], rn = ROW_NUMBER() OVER 
        (PARTITION BY [Key] ORDER BY DateField)
        FROM #t
    )
    SELECT [Key], [Field Aggregate] = STRING_AGG(
        CONCAT(rn, ': ', [Field]), ', ')
        WITHIN GROUP (ORDER BY rn) 
      FROM cte
      GROUP BY [Key] ORDER BY [Key];
    
    • Example db<>fiddle
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with composite primary key. I am trying to load data
My table have data structure like this cate_id task_id date_start date_end other 34 14
I have a table of data with survey results, and I want to do
I have a table schema which has some meta field data which I need
I have a MySQL table which I want to populate with some dummy data
if i have a table like this tbl_attributes --------------- id_attrib (auto incremented, primary key)
I have a data table and I want to perform a case insensitive group
I'm building a Jquery/JS filtered data table. I have a search input field that
I have table of data that is sorted as follows: Item | Sample |
I have table in data base name train delay, with columns train number(int), DelayTime(int),

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.