0

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

我们有表格ordersorder_itemsproducts和。Orders 是主订单表。Order_items 表以特定顺序保存已订购产品的列表。Products 是主要的 products 表,deliveries/delivery_items 表保存已交付的 order_items 列表(我们可以交付整个订单或仅部分交付)。deliveriesdelivery_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
...

我们的生产需求视图在即将到来的一周计划中按数量列出所有未交付的订单项(产品)。我需要的是这样的(这里的数量是随机的):

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 字段。该查询需要从所有未结订单中获取订购的产品数量,检查其中一些产品是否可能已经交付并减去交付数量,最后对结果数量进行排序,如上所示。

更新:这是具有上述结构的 SQL Fiddle 和一些数据http://sqlfiddle.com/#!2/88891/4

4

2 回答 2

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 的格式。

  • 合并,获取无限系列中的第一个非空值。
  • FROM_UNIXTIME 转换为有效的日期时间,以便使用 datediff 进行比较。
  • else 语句处理没有数据匹配条件的情况,因此评估为 0。
  • where 子句消除了所有已交付或超过所有已交付项目的订单。(因此关闭的物品仍将包括在内,因为它们尚未完全发货!)
于 2013-05-30T13:10:21.863 回答
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

您需要做什么才能将它们联系在一起?

于 2013-05-30T13:02:40.813 回答