HeelNGville
Registered User.
- Local time
- Today, 09:45
- Joined
- Apr 13, 2004
- Messages
- 71
Preface this with thanks for any assistance in resolving what I hope is a relatively easy fix.
So, I have a table (sellable_prod_inv) with products (column_name: product_id) and inventory date (column_name: inventory_dt (mm/dd/yyyy), which contains a snapshot of inventory values by day, dating back many years. I am trying to return the oldest inventory date record, particularly in instances in which inventory is depleted to -0-, then restocked.
Ex
- if I have an item that was in inventory on 1/1/2020 and remained in inventory until the item was depleted to 0 on 3/1/2020, and later returned to stock on 4/1/2020, I would like the results of my SQL to show 4/1/2020 as the most recent inventory date. I cannot use the MIN function, as that would return my initial date of
1/1/2020 and inaccurately reporting current product aging. In this scenario, my aging result would be current date - most recent inventory date of 4/1/2020 to arrive at 6 days .
If an item is constantly in stock and never reaches -0-, then great, min works fine. However, it is the instances in which stock is depleted to -0-, then later restocked at a later date(s) that I am struggling with obtaining the values, as I need the most recent restock date.
Not really sure how to resolve? Some type of row_number over partition? Not certain.
Thanks again for any assistance!
So, I have a table (sellable_prod_inv) with products (column_name: product_id) and inventory date (column_name: inventory_dt (mm/dd/yyyy), which contains a snapshot of inventory values by day, dating back many years. I am trying to return the oldest inventory date record, particularly in instances in which inventory is depleted to -0-, then restocked.
Ex
- if I have an item that was in inventory on 1/1/2020 and remained in inventory until the item was depleted to 0 on 3/1/2020, and later returned to stock on 4/1/2020, I would like the results of my SQL to show 4/1/2020 as the most recent inventory date. I cannot use the MIN function, as that would return my initial date of
1/1/2020 and inaccurately reporting current product aging. In this scenario, my aging result would be current date - most recent inventory date of 4/1/2020 to arrive at 6 days .
If an item is constantly in stock and never reaches -0-, then great, min works fine. However, it is the instances in which stock is depleted to -0-, then later restocked at a later date(s) that I am struggling with obtaining the values, as I need the most recent restock date.
Not really sure how to resolve? Some type of row_number over partition? Not certain.
Thanks again for any assistance!