I have a dataset with warehouse locations, dates, and inventory levels for given warehouse at a given date.
How do I structure a SQL query where I end up with a list of distinct locations and their inventory levels at the earliest date per warehouse? I'm thinking something like...
SELECT warehouse_id, (inventory_amt where min(date) is TRUE) as inventory
FROM inventory_table
GROUP BY warehouse_id
I like using row_number()
and top (1) with ties
for this:
SELECT TOP (1) WITH TIES it.*
FROM inventory_table it
ORDER BY ROW_NUMBER() OVER (PARTITION BY warehouse_id ORDER BY date DESC);
You can also express this as:
select it.*
from inventory_table it
where it.date = (select max(it2.date)
from inventory_table it2
where it2.warehouse_id = it.warehouse_id
);
try something like this
select warehouse_id,....
from inventory_table t1
inner join (
select warehouse_id, min(date) from inventory_table group py warehouse_id
) min
on t1.warehouse_id=min.warehouse_id
This worked for me:
proc sql;
CREATE TABLE inventory_by_warehouse AS
SELECT
a.warehouse_id
,a.inventory_amt
FROM inventory_table as a
WHERE a.date =
(SELECT min(date)
FROM inventory_table
WHERE warehouse_id = a.warehouse_id);
quit;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.