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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:14:14+00:00 2026-06-01T19:14:14+00:00

I want to merge rows in SQL server as follows. Table PK F1 F2

  • 0

I want to merge rows in SQL server as follows.

Table

PK  F1      F2      F3      Order
A   NULL    NULL    Grapes  2
B   NULL    Fig     NULL    1
C   Apple   Orange  Banana  0

The input and the expected result is as follows.

Input       Expected Result     
2       Apple   Fig    Grapes
1       Apple   Fig    Banana
0       Apple   Orange Banana

How can I do this in sql stored procedure? I prefer to do in Sql other than in code.

Thanks in Advance

  • 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-01T19:14:16+00:00Added an answer on June 1, 2026 at 7:14 pm

    If I understand you input right then maybe something like this:

    Test data

    DECLARE @T TABLE
    (
        PK VARCHAR(5),
        F1 VARCHAR(10),
        F2 VARCHAR(10),
        F3 VARCHAR(10),
        [Order] INT
    )
    
    INSERT INTO @T
    VALUES
        ('A',NULL,NULL,'Grapes',2),
        ('B',NULL,'Fig',NULL,1),
        ('C','Apple','Orange','Banana',0)
    

    Query

    ;WITH CTE
    AS
    (   
        SELECT
            ROW_NUMBER() OVER(ORDER BY [Order]) AS RowNbr,
            T.*
        FROM
            @T AS T
    )
    , CTE2
    AS
    (
        SELECT
            CTE.RowNbr,
            CTE.PK,
            CTE.F1,
            CTE.F2,
            CTE.F3,
            CTE.[Order]
        FROM
            CTE
        WHERE
            CTE.RowNbr=1
        UNION ALL
        SELECT
            CTE.RowNbr,
            CTE.PK,
            ISNULL(CTE.F1,CTE2.F1),
            ISNULL(CTE.F2,CTE2.F2),
            ISNULL(CTE.F3,CTE2.F3),
            CTE.[Order]
        FROM
            CTE
            JOIN CTE2 ON CTE.RowNbr=CTE2.RowNbr+1
    
    )
    SELECT
        CTE2.[Order],CTE2.F1,CTE2.F2,CTE2.F3
    FROM
        CTE2
    ORDER BY
        CTE2.RowNbr DESC
    

    Result

    2   Apple   Fig     Grapes
    1   Apple   Fig     Banana
    0   Apple   Orange  Banana
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to update rows of a table in a specific order, like one
Possible Duplicate: Merge two rows in SQL I have the following values in table:
I want to merge rows from one table to another row in another table
I have my data table in R. I want to merge rows which have
I need to delete many rows from sql server 2008 database, it must be
I want to merge two rows and add a column to show the count
I want to use ExecuteReader with CommandBehavior to collect data from an SQL table
Suppose I have a table with 2 rows by 2 columns. I then merge
Using SQL Server, have a name value pair table. Each row is basically userid,
I want to be able to merge rows and find out how many are

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.