Microsoft
Software
Hardware
Network
Question : better way to retreive a value for the last entry in a table
I have tables related to purchases that hold products and qty ordered values for each customer order.
Im trying to return a value for qty the last time an order was placed for a particular product.
The follow code works but I'd like to know a better way to do it please?
-- find the last purchase for a product for each customer that placed an order within a specific time frame
SELECT MAX(dbo.tbPurchaseItem.iPu
rchaseId) AS LastPurchase, dbo.tbPurchase.iCustomerId
into #tmp
FROM dbo.tbProduct x INNER JOIN
dbo.tbPurchaseItem ON x.iProductId = dbo.tbPurchaseItem.iProduc
tId INNER JOIN
dbo.tbPurchase ON dbo.tbPurchaseItem.iPurcha
seId = dbo.tbPurchase.iPurchaseId
WHERE (x.iProductId = 11 OR
x.iProductId = 22) AND (dbo.tbPurchase.dtPurchase
> '01 mar 2010')
GROUP BY dbo.tbPurchase.iCustomerId
-- find the qty ordered for the products in the last order
SELECT dbo.tbPurchaseItem.iQty as TotalQty, dbo.tbPurchase.iCustomerId
FROM dbo.tbProduct x INNER JOIN
dbo.tbPurchaseItem ON x.iProductId = dbo.tbPurchaseItem.iProduc
tId INNER JOIN
dbo.tbPurchase ON dbo.tbPurchaseItem.iPurcha
seId = dbo.tbPurchase.iPurchaseId
WHERE (x.iProductId = 11 OR
x.iProductId = 22) and dbo.tbPurchaseItem.iPurcha
seId in
(select LastPurchase
from #tmp)
Thanks!
Answer : better way to retreive a value for the last entry in a table
In SAP, there is no total summed amount for Purchase Requisition...
Random Solutions
How do I converting a regedit export to a VB Script?
Jquery Prevent Focus when Clicked
Nutritional spreadsheet: multiple drop down list problems!
Error message when execute Macro
Different between HP BladeSystem, ProLaint DL and ProLiant ML server
BSOD 0x0000007B during Windows 2003 installation on Dell PowerEdge 2970 server
SQL - Simple Select Statement Returns Error "Conversion of nvarchar value overflowed an int column"
IP Fax and Cisco UCM 8
DFS Replication Configuration Sources
Windows VPN through Cisco 1721