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 に答える