2

在数据库中为慢动作者构建报告。我想知道 180 天内未购买的商品并显示最近的购买日期。日期以日期时间格式 mm/dd/yyyy HH:MM:SS.000 存储这是我最近的尝试,我正在尝试自学 sql 以帮助工作,因此感谢任何帮助和解释。数据库是 MS SQL。

SELECT
    Inventory.LocalSKU, 
    InventorySuppliers.SupplierSKU, 
    MAX([Order Details].DetailDate)

FROM            
    Inventory INNER JOIN
    InventorySuppliers ON Inventory.LocalSKU = InventorySuppliers.LocalSKU 
    INNER JOIN
    [Order Details] ON InventorySuppliers.LocalSKU = [Order Details].SKU 
    CROSS JOIN
    POHistory

WHERE     
    GETDATE() >= CONVERT(date,DATEADD(DAY,+30,[Order Details].DetailDate))
ORDER  BY
    [Order Details].DetailDate DESC
4

3 回答 3

0

你能试一下吗:

SELECT
    Inventory.LocalSKU, 
    InventorySuppliers.SupplierSKU, 
    MAX([Order Details].DetailDate)

FROM            
    Inventory INNER JOIN
    InventorySuppliers ON Inventory.LocalSKU = InventorySuppliers.LocalSKU 
    INNER JOIN
    [Order Details] ON InventorySuppliers.LocalSKU = [Order Details].SKU 
    CROSS JOIN
    POHistory

WHERE     
    GETDATE() >= CONVERT(date,DATEADD(DAY,180,[Order Details].DetailDate))
    GROUP BY
            Inventory.LocalSKU, 
            InventorySuppliers.SupplierSKU, 
ORDER  BY
    MAX([Order Details].DetailDate) DESC
于 2013-09-26T03:49:36.980 回答
0

像这样的东西(不检查语法):

SELECT
    Inventory.LocalSKU, 
    InventorySuppliers.SupplierSKU, 
    MAX([Order Details].DetailDate)

FROM            
    Inventory 
    INNER JOIN InventorySuppliers ON Inventory.LocalSKU = InventorySuppliers.LocalSKU 
    INNER JOIN [Order Details] ON InventorySuppliers.LocalSKU = [Order Details].SKU 
    CROSS JOIN POHistory --what is this here for?
GROUP BY
    Inventory.LocalSKU, 
    InventorySuppliers.SupplierSKU
HAVING     
    --your question said 180 days, but your code had 30 days?
    GETDATE() >= CONVERT(date,DATEADD(DAY,180,MAX([Order Details].DetailDate)))
ORDER  BY
    [Order Details].DetailDate DESC
于 2013-09-26T03:53:04.087 回答
0

这是我最终用来识别慢动作和零动作的完整代码,感谢大家的帮助。

    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 
于 2013-09-26T23:32:06.993 回答