2013-01-16 258 views
1

更新:現在正確地爲每個項目提供最近交付的供應商名稱。我現在遇到的問題是它沒有提取沒有先前交付的記錄(空白dlvry_dt)。有誰知道如何解決這一問題?SQL最新日期記錄

非常感謝您的幫助!

;WITH Items As(
Select 
a.bin, a.item, d.item_description As 'Vintage', a.min_reorder, 
c.minor_item_class, c.minor_class_description, a.qty_available, 
a.reorder_threshold As 'Par', a.avg_unit_cost, 
a.qty_available*a.avg_unit_cost As 'Valuation', e.dlvry_dt, g.supplier, 
g.supplier_name, 
RowNum = ROW_NUMBER() OVER(PARTITION BY a.item ORDER BY dlvry_dt DESC) 

from argus.STORE_INVENTORY a, argus.MAJOR_ITEM_CLASS b, 
argus.MINOR_ITEM_CLASS c, argus.ITEM_MASTER d, argus.DELIVERY e 

inner join argus.delivery_line_item f 
on e.delivery = f.delivery 
and e.purchase_order = f.purchase_order 
and e.customer = f.customer 
and e.store = f.store 
and e.supplier = f.supplier 
full outer join argus.supplier g 
on e.supplier = g.supplier 


where a.customer = 10005 
and a.store = 1 
and d.item = a.item 
and b.major_item_class = c.major_item_class 
and d.minor_item_class = c.minor_item_class 
and (b.major_item_class = 15 or b.major_item_class = 17 or b.major_item_class = 14) 
and (c.minor_item_class = 830 or c.minor_item_class = 175 or c.minor_item_class = 880 or c.minor_item_class = 661 or c.minor_item_class = 651 or c.minor_item_class = 785 or c.minor_item_class = 716 or c.minor_item_class = 810 or c.minor_item_class = 850 or c.minor_item_class = 885 or c.minor_item_class = 998 or c.minor_item_class = 840 or c.minor_item_class = 855 or c.minor_item_class = 280) 
and f.line_item_status <> 'D' 
and e.customer = 10005 
and e.store = 1 
and f.item = a.item 
--and (c.minor_item_class = 176 or c.minor_item_class = 651 or c.minor_item_class = 661 or c.minor_item_class = 716 or c.minor_item_class = 810 or c.minor_item_class = 830 or c.minor_item_class = 840 or c.minor_item_class = 850 or c.minor_item_class = 855 or c.minor_item_class = 885 or c.minor_item_class = 998) 
Group By c.minor_class_description, a.bin, a.item, d.item_description, a.min_reorder, a.qty_available, a.reorder_threshold, a.avg_unit_cost, c.minor_item_class, e.dlvry_dt, g.supplier, g.supplier_name 

) 
Select bin, item, Vintage, min_reorder, minor_item_class, minor_class_description, qty_available, Par, avg_unit_cost, Valuation, dlvry_dt, supplier, supplier_name 
From Items 
Where RowNum =1 
Order By minor_class_description 
+1

您使用的是哪種數據庫平臺? MS SQL有'TOP'運算符,它可以讓你獲得按日期值排序的最高記錄,例如'選擇TOP 1值從MyTable ORDER BY TheDate DESC'。 –

回答

2

一種方法是使用CTE(公用表表達式)。

使用此CTE,您可以按照某些條件(即,您的Item)對數據進行分區,並且SQL Server編號的所有行都從1開始,針對每個「分區」,按照某些標準排序。

因此,嘗試這樣的事:

;WITH Items AS 
(
    SELECT 
     Item, Vintage, Qty, DeliveryDate, 
     RowNum = ROW_NUMBER() OVER(PARTITION BY Item ORDER BY DeliveryDate DESC) 
    FROM 
     dbo.YourTableHere -- possibly several JOINs 
    WHERE 
     ...... 
) 
SELECT 
    Item, Vintage, Qty, DeliveryDate 
FROM 
    Items 
WHERE 
    RowNum = 1 

在這裏,我只選擇了「第一」爲每個「分區」項(即每個Item) - 由降DeliveryDate有序。

這是否接近你要找的?

更新:,如果你想在DeliveryDate可能NULL條目,也可以使用類似

 RowNum = ROW_NUMBER() OVER(PARTITION BY Item ORDER BY ISNULL(DeliveryDate, '99991231' DESC) 

NULL爲31日 - 12月9999日期 - 那些永遠是第一位的當按降序排列時。

+0

這讓我如此親近!非常感謝!我要編輯我的原始代碼以顯示我的更新查詢。我現在遇到的問題是它不顯示返回空白交付日期的結果,但我希望顯示這些結果。有什麼想法嗎?再次感謝! – fullOfQuestions

+0

@fullOfQuestions:所以「空白」交付日期(你可能意味着包含'NULL')應該是「最近的」? –

+0

類別。目標是拉動庫存中的每一件商品,同時拉動最近的交貨。如果某個商品處於庫存狀態,但沒有交貨記錄,則仍需要顯示,但顯示最新供應商的頁面部分將爲空白。那有意義嗎? – fullOfQuestions