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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:27:21+00:00 2026-05-16T05:27:21+00:00

I need to produce the column required in the following table using SQL without

  • 0

I need to produce the column “required” in the following table using SQL without using loops and correlated sub queries. Is this possible in SQL 2008?

Date    Customer   Value   Required   Rule
20100101       1      12         12
20100101       2                  0   If no value assign 0
20100101       3      32         32
20100101       4      42         42
20100101       5      15         15
20100102       1                 12   Take last known value
20100102       2                  0   Take last known value
20100102       3      39         39
20100102       4                 42   Take last known value
20100102       5      16         16
20100103       1      13         13
20100103       2      24         24
20100103       3                 39   Take last known value
20100103       4                 42   Take last known value
20100103       5      21         21
20100104       1      14         14
20100104       2                 24   Take last known value
20100104       3                 39   Take last known value
20100104       4      65         65
20100104       5      23         23

Basically I am filling empty “Value” cells with last know value for that customer. Remember the last row may not have a valid value, so you will have to pick it from the row before that with a valid value.

  • 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-16T05:27:21+00:00Added an answer on May 16, 2026 at 5:27 am

    Faiz,

    how about the following query, it does what you want as far as I understand it. The comments explain each step. Take a look at CTEs on Books Online. This example could even be changed to use the new MERGE command for SQL 2008.

    /* Test Data & Table */
    DECLARE @Customers TABLE
        (Dates datetime,
         Customer integer,
         Value integer) 
    
        INSERT  INTO @Customers
        VALUES  ('20100101', 1, 12),
            ('20100101', 2, NULL),
            ('20100101', 3, 32),
            ('20100101', 4, 42),
            ('20100101', 5, 15),
            ('20100102', 1, NULL),
            ('20100102', 2, NULL),
            ('20100102', 3, 39),
            ('20100102', 4, NULL),
            ('20100102', 5, 16),
            ('20100103', 1, 13),
            ('20100103', 2, 24),
            ('20100103', 3, NULL),
            ('20100103', 4, NULL),
            ('20100103', 5, 21),
            ('20100104', 1, 14),
            ('20100104', 2, NULL),
            ('20100104', 3, NULL),
            ('20100104', 4, 65),
            ('20100104', 5, 23) ;
    
    /* CustCTE - This gives us a RowNum to allow us to build the recursive CTE CleanCust */
    WITH    CustCTE
              AS (SELECT    Customer,
                            Value,
                            Dates,
                            ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Dates) RowNum
                  FROM      @Customers),
    
    /* CleanCust - A recursive CTE. This runs down the list of values for each customer, checking the Value column, if it is null it gets the previous non NULL value.*/
            CleanCust
              AS (SELECT    Customer,
                            ISNULL(Value, 0) Value, /* Ensure we start with no NULL values for each customer */
                            Dates,
                            RowNum
                  FROM      CustCte cur
                  WHERE     RowNum = 1
                  UNION ALL
                  SELECT    Curr.Customer,
                            ISNULL(Curr.Value, prev.Value) Value,
                            Curr.Dates,
                            Curr.RowNum
                  FROM      CustCte curr
                  INNER JOIN CleanCust prev ON curr.Customer = prev.Customer
                                               AND curr.RowNum = prev.RowNum + 1)
    
    /* Update the base table using the result set from the recursive CTE */
        UPDATE trg
        SET Value = src.Value
        FROM    @Customers trg
        INNER JOIN CleanCust src ON trg.Customer = src.Customer
                                    AND trg.Dates = src.Dates
    
    /* Display the results */
    SELECT * FROM @Customers
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish I could figure this out. I need to produce a table with
Morning- I need a function that would produce the following kind of looking sequence:
I am using StreamingMarkupBuilder to produce XML and I need to have a tag.
In my small project I'm using System.Reflection classes to produce executable code. I need
I need to produce a SQL report showing the number of times a particular
I need to produce a table similar to: What we see is the left
I need an excel formula for this to produce the values on the right
Please help! I need to produce a Crystal Report with multiple data columns, but
I need to produce somewhat complicated PDFs from a Rails app - tables with
I need to produce an id surrounded by braces ( for example {1234} ).

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.