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 8478337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:43:04+00:00 2026-06-10T18:43:04+00:00

In a sqlserver table of 200,000 plus records, i need help with faster concat

  • 0

In a sqlserver table of 200,000 plus records, i need help with faster concat of some columns data grouped by other columns. For example, sample data and expected results is shown below.

Here i need to concat the last columns ColumnA, ColumnB, ColumnC as pipe-delimited string, for the combination of these four like :

where KeyNumber=@strKeyNumber  and  Action=@strAction  and  Type=@strType  and  Code=@strCode

These four are the distinct combination.

I coded for this using t-sql STUFF function, but it is too slow. I also coded differently with while loop, but even that is too slow.

So i need help with getting a faster result.

The data in those columns A, B, C are long strings, so the concatenated string should be of type nvarchar(max).

Original data in table (duplicates can be there in some columns):

ID  KeyNumber   Action  Type    Code    ColumnA     ColumnB     ColumnC  
1   1111111111  AC1     TYPE1   CODE1   ValueA1     ValueB1     ValueC1 
2   1111111111  AC1     TYPE1   CODE1   ValueA2     ValueB2     ValueC2 
3   1111111111  AC1     TYPE1   CODE1   ValueA2     ValueB2     ValueC3 
4   1111111111  AC1     TYPE1   CODE1   ValueA3     ValueB3     ValueC4
5   2222222222  AC2     TYPE2   CODE2   ValA1       ValB1       ValC1   
6   2222222222  AC2     TYPE2   CODE2   ValA2       ValB2       ValC2
7   2222222222  AC2     TYPE2   CODE2   ValA3       ValB3       ValC3
8   2222222222  AC2     TYPE2   CODE2   ValA4       ValB4       ValC4
9   2222222222  AC2     TYPE2   CODE2   ValA4       ValB5       ValC4   

Need the result data into a new table like below (duplicate values in above table should not be repeated here):

ID  KeyNumber   Action  Type    Code    ColumnA                 ColumnB                         ColumnC  
1   1111111111  AC1     TYPE1   CODE1   ValueA1|ValueA2|ValueA3 ValueB1|ValueB2|ValueB3         ValueC1|ValueC2|ValueC3|ValueC4 
2   2222222222  AC2     TYPE2   CODE2   ValA1|ValA2|ValA3|ValA4 ValB1|ValB2|ValB3|ValB4|ValB5   ValC1|ValC2|ValC3|ValC4 
  • 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-10T18:43:06+00:00Added an answer on June 10, 2026 at 6:43 pm

    You can check this one:

    DECLARE @Table TABLE
    (
        ID BIGINT,
        Keynumber BIGINT,
        [Action] CHAR(3),
        [Type] CHAR(5),
        Code CHAR(5),
        ColumnA NVARCHAR(MAX),
        ColumnB NVARCHAR(MAX),
        ColumnC NVARCHAR(MAX)
    )
    
    INSERT INTO @TABLE(ID,Keynumber,[Action],[Type],Code,ColumnA,ColumnB,ColumnC)
    VALUES   (1,1111111111,'AC1','TYPE1','CODE1','ValueA1','ValueB1','ValueC1') 
            ,(2,1111111111,'AC1','TYPE1','CODE1','ValueA2','ValueB2','ValueC2')
            ,(3,1111111111,'AC1','TYPE1','CODE1','ValueA2','ValueB2','ValueC3')
            ,(4,1111111111,'AC1','TYPE1','CODE1','ValueA3','ValueB3','ValueC4')
            ,(5,2222222222,'AC2','TYPE2','CODE2','ValA1','ValB1','ValC1')
            ,(6,2222222222,'AC2','TYPE2','CODE2','ValA2','ValB2','ValC2')
            ,(7,2222222222,'AC2','TYPE2','CODE2','ValA3','ValB3','ValC3')
            ,(8,2222222222,'AC2','TYPE2','CODE2','ValA4','ValB4','ValC4')
            ,(9,2222222222,'AC2','TYPE2','CODE2','ValA4','ValB5','ValC4')
    
    SELECT   Keynumber
            ,[Action]
            ,[Type]
            ,Code
            ,(
                 SELECT  ColumnA AS [text()]
                 FROM    @Table TableOne
                 WHERE    TableOne.Keynumber = TableTwo.Keynumber and  TableOne.[Action] = TableTwo.[Action] and  TableOne.[Type] = TableTwo.[Type]
                 ORDER BY TableOne.ColumnA
                 FOR XML PATH(''), TYPE
            ).value('/', 'NVARCHAR(MAX)') AS ColumnA
            ,(
                 SELECT  ColumnB AS [text()]
                 FROM     @Table TableOne
                 WHERE    TableOne.Keynumber = TableTwo.Keynumber and  TableOne.[Action] = TableTwo.[Action] and  TableOne.[Type] = TableTwo.[Type]
                 ORDER BY TableOne.ColumnB
                 FOR XML PATH(''), TYPE
            ).value('/', 'NVARCHAR(MAX)') AS ColumnB
            ,(
                SELECT   ColumnC AS [text()]
                FROM     @Table TableOne
                WHERE    TableOne.Keynumber = TableTwo.Keynumber and  TableOne.[Action] = TableTwo.[Action] and  TableOne.[Type] = TableTwo.[Type]
                ORDER BY TableOne.ColumnC
                FOR XML PATH(''), TYPE
            ).value('/', 'NVARCHAR(MAX)') AS ColumnC
    
    FROM(
            SELECT  DISTINCT Keynumber,[Action],[Type],Code
            FROM    @Table
        ) TableTwo
    

    Also, I am not sure what method of string concatenation you are using. You can check this site for other techniques:

    http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need management 10,000 GPS devices, each GPS device upload a GPS data every
I need some help is MS SQL Server Query. I’m not much of a
I have a sql DB table columns which carries data in 0000-00-0000 format. For
I have a SqlServer 2008 table which has a Primary Key (IsIdentity=Yes) and three
I've a database in SQLServer 2008. I've a table Table1 with three rows as
I have a SQL Server table which has 625 columns created with different datatypes
I have a SQL Server table with 2 columns, Code and CodeDesc. I want
I got a SQL Server table with columns ResolvedDate and ResolvedBy . Now I
I am using a simple table with 6 columns, 3 of which are of
Suppose I have a table with columns (DayId, RunningTotal): DayId RunningTotal --------------------- 1 25

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.