基本上试图从每个ItemCode获取LastReceiptDate和QuantityPurchased,但没有成功。下面是我从桌子上拿到的东西
awk -F': ' '/Patch[0-9].*.*/ && !a[$2]++' Input_file
这就是我想要的结果
+----------+-------------+-------------------+
| ItemCode | ReceiptDate | QuantityPurchased |
+----------+-------------+-------------------+
| 107082 | 2018-10-15 | 1200 |
| 107082 | 2018-07-11 | 360 |
| 107082 | 2018-06-18 | 2760 |
| 107082 | 2018-05-30 | 1200 |
| 05505501 | 2018-05-11 | 576 |
| 107082 | 2018-05-11 | 1056 |
| 05505501 | 2018-04-20 | 600 |
| 107082 | 2018-04-20 | 600 |
| 05505501 | 2018-04-06 | 384 |
| 107082 | 2018-04-06 | 600 |
| 05505501 | 2018-02-19 | 1200 |
| 05505501 | 2018-02-06 | 1200 |
| 107082 | 2018-02-06 | 600 |
| 05505501 | 2018-02-02 | 600 |
| 107082 | 2018-02-02 | 600 |
| 05505501 | 2018-01-12 | 1200 |
+----------+-------------+-------------------+
我尝试过的
+----------+-------------------------------+-------------+------------+---------------------+---------------+-----------------+------------------+-------------------+
| ItemCode | ItemCodeDesc | LastDatePur | LastQtyPur | TotalQuantityOnHand | Qty_0_90_Days | Qty_91_180_Days | Qty_181_270_Days | Qty_Over_270_Days |
+----------+-------------------------------+-------------+------------+---------------------+---------------+-----------------+------------------+-------------------+
| 05505501 | MDSBS-1-0 FANTA SALAD BWL CLR | 2018-05-11 | 576 | 3870 | 0 | 0 | 576 | 0 |
| 107082 | MECRTL-6-00-6pc Round Acrylic | 2018-10-15 | 1200 | 3038 | 1200 | 0 | 0 | 0 |
+----------+-------------------------------+-------------+------------+---------------------+---------------+-----------------+------------------+-------------------+
我知道下面的行是问题所在。尝试了子查询和分组,但是它们最终都会出错。任何帮助将不胜感激。
SELECT DISTINCT ph.ItemCode, ph.ItemCodeDesc, MD.LastDatePur, MQ.LastQtyPur,
i.TotalQuantityOnHand,
(CASE WHEN DATEDIFF(CURDATE(), MD.LastDatePur) BETWEEN 0 AND 90 THEN MQ.LastQtyPur ELSE 0 END) AS Qty_0_90_Days,
(CASE WHEN DATEDIFF(CURDATE(), MD.LastDatePur) BETWEEN 91 AND 180 THEN MQ.LastQtyPur ELSE 0 END) AS Qty_91_180_Days,
(CASE WHEN DATEDIFF(CURDATE(), MD.LastDatePur) BETWEEN 181 AND 270 THEN MQ.LastQtyPur ELSE 0 END) AS Qty_181_270_Days,
(CASE WHEN DATEDIFF(CURDATE(), MD.LastDatePur) > 270 THEN MQ.LastQtyPur ELSE 0 END) AS Qty_Over_270_Days
FROM po_purchaseshistory ph, ci_item i,
(SELECT ph.ItemCode, MAX(ph.ReceiptDate) AS LastDatePur FROM po_purchaseshistory ph WHERE ph.QuantityPurchased != 0 GROUP BY ph.ItemCode) AS MD,
(SELECT ph.ItemCode, ph.QuantityPurchased AS LastQtyPur FROM po_purchaseshistory ph WHERE ph.QuantityPurchased != 0 ORDER BY ph.ReceiptDate DESC LIMIT 1) AS MQ
WHERE ph.ItemCode = i.ItemCode
AND ph.ItemCode = MD.ItemCode
AND ph.ItemCode = MQ.ItemCode
AND ph.ItemCode IN (107082, 05505501)
答案 0 :(得分:0)
尝试以下方法:
SELECT P.* FROM
(
SELECT ItemCode,MAX(ReceiptDate) AS LastDate FROM po_purchaseshistory GROUP BY ItemCode
)A,po_purchaseshistory P WHERE P.ItemCode = A.ItemCode AND A.LastDate = P.ReceiptDate
答案 1 :(得分:0)
和有效的代码。
SELECT DISTINCT ph.ItemCode, ph.ItemCodeDesc, MD.LastDatePur, MQ.LastQtyPur,
i.TotalQuantityOnHand,
(CASE WHEN DATEDIFF(CURDATE(), MD.LastDatePur) BETWEEN 0 AND 90 THEN MQ.LastQtyPur ELSE 0 END) AS Qty_0_90_Days,
(CASE WHEN DATEDIFF(CURDATE(), MD.LastDatePur) BETWEEN 91 AND 180 THEN MQ.LastQtyPur ELSE 0 END) AS Qty_91_180_Days,
(CASE WHEN DATEDIFF(CURDATE(), MD.LastDatePur) BETWEEN 181 AND 270 THEN MQ.LastQtyPur ELSE 0 END) AS Qty_181_270_Days,
(CASE WHEN DATEDIFF(CURDATE(), MD.LastDatePur) > 270 THEN MQ.LastQtyPur ELSE 0 END) AS Qty_Over_270_Days
FROM po_purchaseshistory ph
LEFT JOIN ci_item i ON ph.ItemCode = i.ItemCode
LEFT JOIN (SELECT ph.ItemCode, MAX(ph.ReceiptDate) AS LastDatePur FROM po_purchaseshistory ph WHERE ph.QuantityPurchased != 0 GROUP BY ph.ItemCode) AS MD ON ph.ItemCode = MD.ItemCode
LEFT JOIN (SELECT ph.ItemCode, ph.ReceiptDate, ph.QuantityPurchased AS LastQtyPur FROM po_purchaseshistory ph WHERE ph.QuantityPurchased != 0) AS MQ ON ph.ItemCode = MQ.ItemCode
WHERE ph.ItemCode IN (107082, 05505501)
AND ph.ReceiptDate = MD.LastDatePur
AND ph.ReceiptDate = MQ.ReceiptDate