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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:59:57+00:00 2026-05-30T11:59:57+00:00

I am working on a Data Warehouse project and the client provides daily sales

  • 0

I am working on a Data Warehouse project and the client provides daily sales data. On-hand quantities are provided in most lines but are often missing. I need help on how to fill those missing values based on prior OH and sales information.

Here’s a sample data:

Line#  Store  Item  OnHand  SalesUnits  DateKey
-----------------------------------------------
1      001    A     100     20          1       
2      001    A     80      10          2       
3      001    A     null    30          3       --[OH updated with 70 (80-10)]
4      001    A     null    5           4       --[OH updated with 40 (70-30)]
5      001    A     150     10          5       --[OH untouched]
6      001    B     null    4           1       --[OH untouched - new item]
7      001    B     80      12          2       
8      001    B     null    10          3       --[OH updated with 68 (80-12]

Lines 1 and 2 are not to be updated because OnHand quantities exist.
Lines 3 and 4 are to be updated based on their preceding rows.
Line 5 is to be left untouched because OnHand is provided.
Line 6 is to be left untouched because it is the first row for Item B

Is there a way I can do this in a set operation? I know I can do it easily using a fast_forward cursor but it will take a long time (15M+ rows).

Thanks for your help!

  • 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-30T11:59:58+00:00Added an answer on May 30, 2026 at 11:59 am

    Test data:

    declare @t table(
    Line# int,  Store char(3),  Item char,  OnHand int,  SalesUnits int, DateKey int
    )
    
    insert @t values
    (1,  '001',  'A',  100,   20, 1),
    (2,  '001',  'A',  80 ,   10, 2),
    (3,  '001',  'A',  null,  30, 3),
    (4,  '001',  'A',  null,   5, 4),
    (5,  '001',  'A',  150,   10, 5),
    (6,  '001',  'B',  null,   4, 1),
    (7,  '001',  'B',  null,   4, 2),
    (8,  '001',  'B',  80,    12, 3),
    (9,  '001',  'B',  null,  10, 4)
    

    Script to populate not using cursor:

    ;with a as
    (
    select Line#,  Store,  Item,  OnHand,  SalesUnits, DateKey, 1 correctdata from @t where DateKey = 1
    union all
    select t.Line#,  t.Store,  t.Item,  coalesce(t.OnHand, a.onhand - a.salesunits),  t.SalesUnits, t.DateKey, t.OnHand from @t t
    join a on a.DateKey = t.datekey - 1 and a.item = t.item and a.store = t.store
    )
    update t
    set OnHand = a.onhand 
    from @t t join a on a.line# = t.line#
    where a.correctdata is null
    

    Script to populate using cursor:

    declare @datekey int, @store int, @item char, @Onhand int, 
    @calculatedonhand int, @salesunits int, @laststore int, @lastitem char
    
    DECLARE sales_cursor 
    CURSOR FOR  
    SELECT datekey+1, store, item, OnHand -SalesUnits, salesunits
    FROM @t sales  
    order by store, item, datekey
    
    OPEN sales_cursor;  
    FETCH NEXT FROM sales_cursor  
    INTO @datekey, @store, @item, @Onhand, @salesunits
    
    WHILE @@FETCH_STATUS = 0 
    BEGIN  
    SELECT @calculatedonhand = case when @laststore = @store and @lastitem = @item 
    then coalesce(@onhand, @calculatedonhand - @salesunits) else null end
    ,@laststore = @store, @lastitem = @item
    
    UPDATE s
    SET onhand=@calculatedonhand
    FROM @t s
    WHERE datekey = @datekey and @store = store and @item = item
    and onhand is null and @calculatedonhand is not null
    
    FETCH NEXT FROM sales_cursor  
    INTO @datekey, @store, @item, @Onhand, @salesunits
    
    END 
    CLOSE sales_cursor; 
    DEALLOCATE sales_cursor; 
    

    I recommand you use the cursor version, I doubt you can get a decent performance using the recursive query. I know people in here hate cursors, but when your table has that size, it can be the only solution.

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

Sidebar

Related Questions

I'm working on a data warehouse project and would like to know how to
Working on a Data Warehouse project, the guy that gave us the tutorial advised
I'm currently working on a project where we have a large data warehouse which
I'm working on a Data Warehouse which, in the end, will require me to
I am working on a project that utilizes .Net API (several .Net DLLs) provided
I have data in a table, but am working on data loading. I want
I am working on a data warehouse system which was upgraded about a year
I am working on a data warehouse and looking for an ETL solution that
The following is the data I am working on: __DATA__ Node 1: 98 debug
I always seem to use Get when working with data (strongly typed or otherwise)

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.