I have looked several places, and maybe I'm just not phrasing the search correctly. I have found similar questions, but none of them answer the question.
I have a table of Sight Inventories (where users walk through the storage area and physically check how many products are on hand). The table handles multiple locations. The table structure (partial, only the information needed) is:
create table location_inventory (
id int unsigned not null auto_increment,
location_id int unsigned references location(location_id),
inventory_item_id int unsigned references inventory_item (inventory_item_id),
inventory_date date comment 'Date the sight inventory was taken',
quantity decimal( 15,2 ) comment 'Number of items found during inventory',
primary key ( id ),
unique (location_id,inventory_item_id,inventory_date)
);
It should be a query of the form:
select
a.location_id location,
a.inventory_item_id inventory_item,
a.inventory_date curr_date,
a.quantity curr_quant,
b.inventory_date prev_date,
b.quantity prev_quant,
a.quantity - b.quantity num_used
from
location_inventory a
left outer join
(
select
location_id,
inventory_item_id,
inventory_date,
quantity
from
location_inventory
where
something
) b
on ( location_id,inventory_item_id )
where
a.inventory_date between DATEA and DATEB
But I haven't gotten it to work.
It is that whole subquery that I'm missing. I've seen several answers where we get the previous date, but none where I can actually retrieve the rest of the values from the previous row; it ends up retrieving the values for the most recent entry in the entire table.