我正在使用mysql,并有一个表格,我们记录库存订单。该表(简化)如下所示:
||id|ordered_date |Date |itemid| status ||
||1 |Jan-4 |Jan-4 | a | ordered ||
||2 |Jan-6 |Jan-6 | b | ordered ||
||3 |Jan-8 |Jan-8 | c | ordered ||
||4 |Jan-8 |Jan-9 | c | shipped ||
||4 |Jan-6 |Jan-9 | b | shipped ||
||4 |Jan-4 |Jan-10| a | shipped ||
我们想要的是每天尚未发货的订单总数,如下所示:
date | inventory
Jan-4 | 1
Jan-5 | 1
Jan-6 | 2
Jan-7 | 2
Jan-8 | 3
Jan-9 | 1
Jan-10| 0
我可以使用如下查询找到任何一天的未发货订单总数:
select count(*) from inventory where
status = "Shipped" AND
date > "Jan-8" AND
ordered_date <= "Jan-8"
但我们需要所有日期的订单数量。并且上述查询存在一个问题,即它找不到尚未发货的订单。所以,任何帮助将不胜感激。
(假设日期列是有效的SQL日期)
答案 0 :(得分:2)
试试这个:
SELECT date, COUNT(*)
FROM inventory
WHERE status != 'shipped'
GROUP BY date ORDER BY date;