SQL根据按周排序的交货日期选择订购的产品数量

时间:2013-05-30 12:30:40

标签: mysql sql

我不确定我的问题是否清楚,但我需要的是非常复杂的查询。我自己无法把它放在一起。

我们有表ordersorder_itemsproductsdeliveriesdelivery_items。订单是主要的订单表。 Order_items表按特定顺序保存订购产品的列表。产品是主要产品表和交付/ delivery_items表保存已交付order_items的列表(我们可以提供整个订单或仅部分)。

这是'精简'表结构:

订单:

ID    ORDER_NUMBER     DELIVERY_DATE    STATUS
-------------------------------------------------
1     2013-00001       Unixtimestamp    Closed
2     2013-00002       Unixtimestamp    Open
...

产品:

ID    CODE
-----------------------
1     Product 1
2     Product 2
3     Product 3
...

订购项目:

ID    ORDER_ID    PRODUCT_ID    QTY
-----------------------------------------
1     1           1             2
2     1           2             5
3     1           3             1
4     2           3             10

交付:

ID    ORDER_ID    DELIVERY_NUMBER     TYPE
---------------------------------------------
1     1           2013-00001          Full
2     2           2013-00002          Partial
...

DELIVERY_ITEMS:

ID    DELIVERY_ID     ORDER_ITEM_ID    QTY
------------------------------------------
1     1               1                2
2     1               2                5
3     1               3                1
4     2               4                5
...

我们的生产要求查看在即将到来的周计划中按数量列出所有未交付的order_items(产品)的位置。我需要的是这样的东西(这里的数量是随机的):

Product     Overdue    W0    W1    W2    W3    W4    W5    Later    Total
-------------------------------------------------------------------------
Product 1   1          2     0     0     0     0     0     0        3
Product 2   0          3     5     1     0     0     0     4        13
Product 3   2          4     0     7     5     0     0     0        18
...

整个视图基于当前时间和订单的delivery_date字段。查询需要从所有未结订单中获取订购的产品数量,检查某些产品是否已经交付并减去交货数量,最后对结果数量进行排序,如上所示。

更新:这里是带有一些数据http://sqlfiddle.com/#!2/88891/4

的上述结构的SQL小提琴

2 个答案:

答案 0 :(得分:4)

---使用提供的小提琴(http://sqlfiddle.com/#!2/88891/34/0

SELECT P.Name,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=0 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) AS Overdue,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=7 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>0  then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W0,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=14 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>7  then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W1,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=21 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>14 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W2,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=28 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>21 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W3,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=35 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>28 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W4,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=42 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>35 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W5,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>42 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as Later,
sum(coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0)) as Total

FROM ORDERS O
INNER JOIN ORDER_ITEMS OI
  ON OI.Order_ID = O.ID
INNER JOIN PRODUCTS P on 
  P.ID = OI.Product_ID
LEFT JOIN DELIVERIES D
  ON D.Order_ID = O.ID
LEFT JOIN DELIVERY_ITEMS DI
  ON DI.Delivery_ID = D.ID
  AND OI.ID = DI.Order_Item_ID
WHERE coalesce(DI.Quantity,0) < OI.Quantity 
GROUP BY P.Name

感谢小提琴。这会处理所有剩余的语法错误。并在结果中包含0格式。

  • Coalesce,在无限系列中获取第一个非null值。
  • FROM_UNIXTIME使用datediff转换为有效日期时间进行比较。
  • else语句处理没有数据匹配critiera的情况,因此0是 评估。
  • where子句删除所有已交付物品的订单或超过订购的所有物品。 (因此,关闭的物品仍将包括在内,因为它们尚未完全装运!)

答案 1 :(得分:0)

为了帮助您入门,这里有几个连接:

SELECT o.ORDER_NUMBER, p.CODE, oi.QTY
FROM orders o
JOIN order_items oi ON oi.ORDER_ID = o.ID
JOIN products p ON p.ID = oi.PRODUCT_ID

SELECT d.DELIVERY_NUMBER, d.ORDER_ID, di.ORDER_ITEM_ID, di.QTY
FROM deliveries d
JOIN delivery_items di ON di.DELIVERY_ID = d.ID

你需要做些什么才能把它们捆绑在一起?