上次购买日期的产品

时间:2010-03-15 09:36:46

标签: sql-server-2000

我想从SQL Server 2000 Database查询。我有两个表。它们是Purchase和PurchaseDetails。我想获得带有上次购买ID的产品记录,但我不能用以下陈述查询。所以请帮助我。

SELECT     TOP 100 PERCENT dbo.Purchase.PurchaseID AS LastOfPurchaseID,  dbo.PurchaseDetails.ProductID, 
                      MAX(dbo.Purchase.PurchaseDate) AS LastOfPurchaseDate
FROM         dbo.Purchase INNER JOIN
                      dbo.PurchaseDetails ON dbo.Purchase.PurchaseID = dbo.PurchaseDetails.PurchaseID 
GROUP BY dbo.PurchaseDetails.ProductID, dbo.Purchase.PurchaseDate,dbo.Purchase.PurchaseID
ORDER BY MAX(dbo.Purchase.PurchaseDate) DESC

2 个答案:

答案 0 :(得分:0)

如果您尝试根据最近输入的订单日期检索产品列表,请使用子查询:

SELECT  Purchase.PurchaseID, Purchase.PurchaseDate, PurchaseDetails.ProductID
FROM      Purchase 
    INNER JOIN PurchaseDetails 
    ON Purchase.PurchaseID = PurchaseDetails.PurchaseID
WHERE   (Purchase.PurchaseDate = (SELECT MAX(PurchaseDate) FROM Purchase))

答案 1 :(得分:-1)

难道你不能在DESC ORDER中按购买ID订购吗?当你尝试用MSSQL做事时,一切都变得有点奇怪......