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

The Archive Base Latest Questions

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

Given a table with the value of individual items in inventory for particular dates,

  • 0

Given a table with the value of individual items in inventory for particular dates, how can I return summary data that reflects the total value at each unique date?

For example, given the following structure:

DECLARE @INVENTORY TABLE(
  EFFECTIVE_DATE DATE NOT NULL,
  ITEM INT NOT NULL,
  VALUE DECIMAL(6,2) NOT NULL
)
INSERT @INVENTORY VALUES ('2011-01-01', 1, 40.01)
INSERT @INVENTORY VALUES ('2011-01-01', 2, 35.01)
INSERT @INVENTORY VALUES ('2011-01-01', 3, 17.01)
INSERT @INVENTORY VALUES ('2011-02-01', 2, 50.01)
INSERT @INVENTORY VALUES ('2011-02-01', 3, 45.01)
INSERT @INVENTORY VALUES ('2011-03-01', 1, 10.01)
INSERT @INVENTORY VALUES ('2011-03-01', 4, 5.01)

What query can be used to result in the following?

EFFECTIVE_DATE  TOTAL_VALUE
2011-01-01      92.03
2011-02-01      135.03
2011-03-01      110.04

The challenge is that the inventory value would continue to be carried into subsequent dates. Notice that 2011-03-01 represents the sum of the most recent value for all four different items though only two of them have a record for that day.

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

    This shows value for items in every date:

    SELECT 
       D.EFFECTIVE_DATE, 
       I.ITEM, 
       COALESCE((SELECT TOP 1 VALUE FROM @INVENTORY II WHERE (II.ITEM = I.ITEM) AND (II.EFFECTIVE_DATE <= D.EFFECTIVE_DATE) ORDER BY EFFECTIVE_DATE DESC),0) VALUE
    FROM (SELECT DISTINCT EFFECTIVE_DATE FROM @INVENTORY) D
    CROSS JOIN (SELECT DISTINCT ITEM FROM @INVENTORY) I
    

    This shows summary:

    SELECT 
      EFFECTIVE_DATE,
      SUM(VALUE)
    FROM
    (
        SELECT 
           D.EFFECTIVE_DATE, 
           I.ITEM, 
           COALESCE((SELECT TOP 1 VALUE FROM @INVENTORY II WHERE (II.ITEM = I.ITEM) AND (II.EFFECTIVE_DATE <= D.EFFECTIVE_DATE) ORDER BY EFFECTIVE_DATE DESC),0) VALUE
        FROM (SELECT DISTINCT EFFECTIVE_DATE FROM @INVENTORY) D
        CROSS JOIN (SELECT DISTINCT ITEM FROM @INVENTORY) I
    ) GROUPED_VALUES
    GROUP BY EFFECTIVE_DATE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a table that is acting as a queue, how can I best configure
I've been given a stack of data where a particular value has been collected
Given the following: declare @a table ( pkid int, value int ) declare @b
Given a table or a temp table, I'd like to run a procedure that
I am trying to populate a row in a table given a key value
I need to query a table for values given a string. The table is
For a given table 'foo', I need a query to generate a set of
I am trying to link two fields of a given table to the same
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Given a table (mytable) containing a numeric field (mynum), how would one go about

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.