0

I am not sure if my question is clear but what I need is quite complex query. I am unable to put it together myself.

We have tables orders, order_items, products, deliveries and delivery_items. Orders is the main order table. Order_items table holds the list of ordered products in certain order. Products is the main products table and deliveries/delivery_items tables hold the list of delivered order_items (we can deliver entire order or only partially).

This is the 'stripped down' table structure:

ORDERS:

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

PRODUCTS:

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

ORDER ITEMS:

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

DELIVERIES:

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
...

Our production demands view where all non-delivered order_items(products) are listed by quantities in the upcoming week schedule. What I need is something like this (quantities here are random):

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
...

The entire view is based on current time and order's delivery_date field. The query would need to get ordered product quantities from all open orders, check if some of there products were maybe already delivered and subtract delivered quantities, and on the end sort the result quantities as shown above.

UPDATE: here is SQL Fiddle with above structure with some data http://sqlfiddle.com/#!2/88891/4

9
  • In deliveries table order_id 1 has type full, I guess this means it's been delivered. Yet it has status open in orders table. Also there seems to be no column to indicate which items of each order might have already been delivered and which not. There's just column type = full/partial which is basically useless. Also post what you've tried. This is not a "we do the work for you" site. And you won't learn to do it yourself if you don't at least try to do it yourself.
    – fancyPants
    May 30, 2013 at 12:47
  • and use sqlfiddle.com to create a basic schema with some values
    – VeNoMiS
    May 30, 2013 at 12:49
  • You are correct regarding delivered order, it was my mistake. That order should be marked closed and all product quantities of that ignored in the final view. I have corrected in my question. Regarding the indication which items have already been delivered. What important are only the desired delivered quantities, and that can be read trough 'delivery_items' table, which is also linked to 'order_items'. May 30, 2013 at 12:52
  • @VeNoMiS ok I will create sqlfiddle May 30, 2013 at 12:53
  • @VeNoMiS SQL Fiddle added May 30, 2013 at 13:16

2 Answers 2

4

---Using Fiddle provided (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

Thanks for the fiddle. This takes care of all the remaining syntax errors. and formats to include 0 in the results.

  • Coalesce, takes first non-null value in an unlimited series.
  • FROM_UNIXTIME converts into to valid date time for comparison using datediff.
  • else statments handle situations when no data matches critiera thus 0 is evaluated.
  • where clause eliminates orders with all items delivered or more than all items ordered delivered. (Thus items closed will still be included since they are not fully shipped!)
13
  • W0 is current week, W1 is next week and so on... Overdue are the orders that delivery date is already in the past... May 30, 2013 at 13:17
  • Delivered item count can never be > then ordered item count... It's just impossible. May 30, 2013 at 13:19
  • Corrected logic again due to compounding error on products being included in weeks following due date. Logically, I think this is correct. It may have a few syntax errors however. And there may be a faster way to do this with Analytic functions; or perhaps a subquery to get the date difference first instead of doing the calculations over and over.
    – xQbert
    May 30, 2013 at 13:25
  • There is a syntax error I can't fix > You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SUM(case WHEN DATEDIFF... May 30, 2013 at 14:09
  • I think case_value is missing after case...? From case syntax docs dev.mysql.com/doc/refman/5.0/en/case.html May 30, 2013 at 14:13
0

To get you started, here are a couple of joins:

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

And:

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

What would you have to do to tie those together?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.