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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:07:16+00:00 2026-06-09T02:07:16+00:00

@@Version 1 Using SQL Server 2008, I am trying to cascade values down a

  • 0

@@Version 1

Using SQL Server 2008, I am trying to cascade values down a column. I have a table with group id (GID) and Seq containing ordering for records within the group. For the columns present, in this case Name and Salary – my real table has over 50 columns, if they contain NULLs I need to update the NULL value with the value from the previous row for that column that contain a non-null value.

Here is something to illustrate this:

GID Seq Name    Salary
1   1   James   NULL
1   2   NULL    100
1   3   NULL    NULL
2   1   NULL    81
2   2   Smith   NULL
2   3   NULL    NULL
3   1   Charles NULL
3   2   NULL    NULL
3   3   Brown   NULL
3   4   NULL    75
4   0   Ron 50
4   1   NULL    20
4   2   NULL    NULL

My result should be:

GID Seq Name    Salary
1   1   James   NULL
1   2   James   100
1   3   James   100
2   1   NULL    81
2   2   Smith   81
2   3   Smith   81
3   1   Charles NULL
3   2   Charles NULL
3   3   Brown   NULL
3   4   Brown   75
4   0   Ron 50
4   1   Ron 20
4   2   Ron 20

I am looking to do this without using dynamic SQL, loops or cursors.

Code for simple test case:

DECLARE @Test TABLE (GID int, Seq int, Name varchar(50), Salary decimal) 

INSERT INTO @Test VALUES (1, 1, 'James', NULL)
INSERT INTO @Test VALUES (1, 2, NULL, 100.40)
INSERT INTO @Test VALUES (1, 3, NULL, NULL)
INSERT INTO @Test VALUES (2, 1, NULL, 80.50)
INSERT INTO @Test VALUES (2, 2, 'Smith', NULL)
INSERT INTO @Test VALUES (2, 3, NULL, NULL)
INSERT INTO @Test VALUES (3, 1, 'Charles', NULL)
INSERT INTO @Test VALUES (3, 2, NULL, NULL)
INSERT INTO @Test VALUES (3, 3, 'Brown', NULL)
INSERT INTO @Test VALUES (3, 4, NULL, 75)
INSERT INTO @Test VALUES (4, 0, 'Ron', 50)
INSERT INTO @Test VALUES (4, 1, NULL, 20)
INSERT INTO @Test VALUES (4, 2, NULL, NULL)

SELECT * FROM @Test

@@Version 2
Thanks GilM for the solution to @@Version 1. I have made a small addition to the problem. The starting number in the Seq column may be either a 0 or 1. In the solution to the first problem the anchor in the recursive CTE refers to 1, what if its either a 1 or 0? The last 3 rows of data (GID = 4) were added to all the above three code blocks in this version.

Thanks!

  • 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-09T02:07:17+00:00Added an answer on June 9, 2026 at 2:07 am

    How about this?:

    ;WITH CTE AS (
    SELECT GID, SEQ, Name, Salary
    FROM @Test t1
    WHERE SEQ = (SELECT MIN(SEQ) FROM @Test t2 WHERE t2.GID = t1.GID)
    UNION ALL
    SELECT t.GID, t.SEQ, COALESCE(t.Name,c.Name), COALESCE(t.Salary,c.Salary)
    FROM CTE c
    JOIN @Test t ON t.GID = c.GID AND t.SEQ = c.SEQ+1
    )
    UPDATE t SET 
        Name = c.Name,
        Salary =  c.Salary
    FROM @Test t
    JOIN CTE c ON c.GID = t.GID AND c.Seq = t.SEQ
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using SQL Server 2008 Enterprise version. We have a large table FooTable
Using FNH w/ SQL Server 2008, I'm trying to add a Version as a
I am using SQL Server 2008 Enterprise version. I want to know if I
I'm trying to insert into XML column (SQL SERVER 2008 R2), but the server's
i am trying to connect with sql server 2008 using java connection string but
I am using SQL Server 2008 Express and I cannot buy the full version
I'm trying to backup a database in SQL Server 2008 and have the database
Introduction I'm trying to query an xml column in SQL server 2008, but I
I have SharePoint 2010 and SQL server 2008 setup on two machines. I have
I am trying to figure how to get SQL Server 2008 to import data

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.