这是我最终用来识别慢动作和零动作的完整代码,感谢大家的帮助。
SELECT suppliers.suppliername,
inventory.localsku,
inventorysuppliers.suppliersku,
inventory.itemname,
inventory.Text2 as "Outlet",
inventory.Text3 as "Season",
inventory.Text5 as "Discount",
inventory.qoh,
Max(CONVERT(DATE, [Order Details].detaildate)) AS [Last Ordered],
Max(CONVERT(DATE, pohistory.date)) AS [Last PO],
inventory.discontinued
FROM inventory
INNER JOIN inventorysuppliers
ON inventory.localsku = inventorysuppliers.localsku
INNER JOIN suppliers
ON inventorysuppliers.supplierid = suppliers.supplierid
INNER JOIN pohistory
ON inventorysuppliers.suppliersku = pohistory.supplierssku
INNER JOIN [Order Details]
ON inventorysuppliers.localsku = [Order Details].sku
WHERE ( Getdate() >= CONVERT(DATE, Dateadd(month, 6, [Order Details].detaildate)) )
AND ( inventory.discontinued = 0 )
AND ( Getdate() <= CONVERT(DATE, Dateadd(month, 5, pohistory.date)) )
AND inventory.discontinued = 0
OR
( [Order Details].detaildate IS NULL )
AND ( inventory.discontinued = 0 )
AND ( Getdate() <= CONVERT(DATE, Dateadd(month, 5, pohistory.date)) )
AND inventory.discontinued = 0
GROUP BY inventory.localsku,
inventorysuppliers.suppliersku,
inventory.itemname,
suppliers.suppliername,
inventory.qoh,
inventory.discontinued,
inventory.Text2,
inventory.Text3,
inventory.Text5
HAVING ( inventory.QOH > 0)
UNION
SELECT suppliers.suppliername,
inventory.localsku,
inventorysuppliers.suppliersku,
inventory.itemname,
inventory.Text2 as "Outlet",
inventory.Text3 as "Season",
inventory.Text5 as "Discount",
inventory.qoh,
Max(CONVERT(DATE, [Order Details].detaildate)) AS [Last Ordered],
Max(CONVERT(DATE, pohistory.date)) AS [Last PO],
inventory.discontinued
FROM inventory
INNER JOIN inventorysuppliers
ON inventory.localsku = inventorysuppliers.localsku
INNER JOIN suppliers
ON inventorysuppliers.supplierid = suppliers.supplierid
INNER JOIN pohistory
ON inventorysuppliers.suppliersku = pohistory.supplierssku
LEFT OUTER JOIN [Order Details]
ON inventorysuppliers.localsku = [Order Details].sku
WHERE ( [Order Details].detaildate IS NULL )
AND ( inventory.discontinued = 0 )
AND ( Getdate() <= CONVERT(DATE, Dateadd(month, 5, pohistory.date)) )
AND inventory.discontinued = 0
GROUP BY inventory.localsku,
inventorysuppliers.suppliersku,
inventory.itemname,
suppliers.suppliername,
inventory.qoh,
inventory.discontinued,
inventory.Text2,
inventory.Text3,
inventory.Text5
HAVING ( inventory.QOH > 0)
ORDER BY suppliers.suppliername