我想根据FIFO到期日期从库存表中获取批次ID。如何获得它。我写了一个查询,它给出了确切的批次,但批次ID不准确。基本上,我想提取批次最先过期的产品
库存表就像
Batch_ID | Product_ID | Quantity | Is_Expiry |Expiry_Date | Location_ID
6 | 148 | 90 | 1 | 2019-08-24 | 1
4 | 148 | 75 | 1 | 2019-09-13 | 1
2 | 148 | 0 | 1 | 2019-07-11 | 1
我写这个查询
Select batch_id,min(datediff(expiry_date,now())) as Remaining_Days From Stock Where Product_ID = '148' and quantity > 0 and Location_ID = '1'
当前输出
Batch_ID | Remaining_Days
4 | 56
预期输出:
Batch_ID | Remaining_Days
6 | 56
答案 0 :(得分:1)
您正在使用汇总最小函数,它会给您错误的输出。您可以使用按函数排序以进行排序。
Select batch_id,(datediff(expiry_date,now())) as Remaining_Days From stock
Where Product_ID = '148' and quantity > 0 and Location_ID = '1'
order by Remaining_days
limit 1;