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.
This shows value for items in every date:
This shows summary: