Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
terbs
Starting Member
29 Posts |
Posted - 2008-09-09 : 22:34:09
|
I need to peform a Select statement which retrieves the most recent Buy price of a certain product from each supplier it has came from.All I can seem to manage so far is a list of the suppliers and the most recent Date.SELECT Max(TBLPurchaseOrder.Date) AS MaxOfDate, TBLPurchaseOrder.SupplierIDFROM TBLPurchaseOrderItem INNER JOIN TBLPurchaseOrder ON TBLPurchaseOrderItem.PONumber = TBLPurchaseOrder.PONumberGROUP BY TBLPurchaseOrderItem.PARTID, TBLPurchaseOrder.SupplierIDHAVING (((TBLPurchaseOrderItem.PARTID)=@PartID))ORDER BY Max(TBLPurchaseOrder.Date) DESC What I need to add to this is SupplierCode, BuyPrice and SupplierName.SupplierCode and BuyPrice are fields belonging to TBLPurchaseOrderItem. SupplierName is a field belonging to TBLSupplier which joins on TBLPurchaseOrder.SupplierID.Any help would be great, cheers |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-09-09 : 22:58:30
|
[code]SELECT P.PARTID, P.SupplierID, P.SupplierName, P.SupplierCode, P.[Date], P.BuyPriceFROM( SELECT i.PARTID, O.SupplierID, S.SupplierName, S.SupplierCode, O.[Date], I.BuyPrice, row_no = row_number() OVER (PARTITION BY i.PARTID, O.SupplierID ORDER BY O.Date DESC) FROM TBLPurchaseOrderItem I INNER JOIN TBLPurchaseOrder O ON I.PONumber = O.PONumber INNER JOIN TblSupplier S ON O.SupplierID = S.SupplierID WHERE I.PARTID = @PartID) PWHERE P.row_no = 1[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
terbs
Starting Member
29 Posts |
Posted - 2008-09-10 : 01:18:15
|
| thanks khtan for the quick reply, but it seems to throw an error on 'row_no'"Msg 102, Level 15, State 1, Line 5Incorrect syntax near '='." |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-09-10 : 01:20:36
|
edited. Add in the missing comma , after the BuyPrice KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
terbs
Starting Member
29 Posts |
Posted - 2008-09-10 : 02:39:32
|
| thank khtan, works a charm. |
 |
|
|
|
|
|
|
|