对于超过180天未购买的商品,需要返回上次购买日期

时间:2013-09-26 03:43:41

标签: sql sql-server date datetime

为数据库中的慢速移动器构建报告。我想知道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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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