If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Products with Last Purchase Date and Last Purchase ID

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-15-10, 05:48
mikewin86 mikewin86 is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
Products with Last Purchase Date and Last Purchase ID

Hello ,

I would like to query from SQL Server 2000 Database.I have got two tables. They are Purchase and PurchaseDetails. I would like to get product records with Last Purchase ID and Last Purchase Date but I can't query with the following statements.So please help me.

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
Reply With Quote
  #2 (permalink)  
Old 03-15-10, 06:07
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
I assume by last you mean greatest.

That query will work if your greatest purchase ID correlates with your greatest purchase date. Otherwise it won't.
Reply With Quote
  #3 (permalink)  
Old 03-15-10, 06:31
mikewin86 mikewin86 is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
In fact , that query must work but it retrieve all records and all products(i.e. not only last Purchase Date but also old Purchase Date).
Reply With Quote
  #4 (permalink)  
Old 03-15-10, 06:46
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
I see.
So is this what you need: "For each product I want to know the greatest purchase date and the ID for that purchase"?
Reply With Quote
  #5 (permalink)  
Old 03-15-10, 06:51
mikewin86 mikewin86 is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
yes friend. here my sample data for this.

Purchase Table includes the following fields.

PurchaseID
PurchaseDate
Remark

Purchase Deatails Table includes the following fields.

PurchaseDetailsID
PurchaseID
ProductID
BatchNo
Quantity
Price
Amount

here sample data

PurchaseID PurchaseDate
100 12/1/2009
101 12/20/2009
102 12/30/2009

PurchaseDetailsID PurchaseID ProductID Quantity BatchNo
200 100 20 100 A10000
201 101 20 120 B10001
202 102 20 50 D01111

I would like to get product with Last Purchase Date and Last Purchase ID as below.

PurchaseID Date Product Qty BatchNo
102 12/30/2009 20 50 D01111
Reply With Quote
  #6 (permalink)  
Old 03-15-10, 06:53
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Code:
SELECT  Purchase.PurchaseID         AS LastOfPurchaseID
      , PurchaseDetails.ProductID
      , Purchase.PurchaseDate       AS LastOfPurchaseDate 
FROM    dbo.Purchase 
INNER JOIN 
        dbo.PurchaseDetails 
ON dbo.Purchase.PurchaseID      = dbo.PurchaseDetails.PurchaseID
INNER JOIN 
        (
            SELECT  PurchaseDetails.ProductID
                  , GreatestPurchaseDate    = MAX(Purchase.PurchaseDate)
            FROM    dbo.PurchaseDetails
            GROUP BY PurchaseDetails.ProductID
        ) AS LastPurchaseByProduct
ON  LastPurchaseByProduct.ProductID             = PurchaseDetails.ProductID
AND LastPurchaseByProduct.GreatestPurchaseDate  = PurchaseDetails.PurchaseDate
Reply With Quote
  #7 (permalink)  
Old 03-15-10, 07:10
mikewin86 mikewin86 is offline
Registered User
 
Join Date: Mar 2010
Posts: 4
Thanks friend. But when I run that statements , it give following error.

"The column prefix 'Purchase' does not match with a table name or alias name used in the query."
Reply With Quote
  #8 (permalink)  
Old 03-15-10, 07:12
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Code:
SELECT  Purchase.PurchaseID         AS LastOfPurchaseID
      , PurchaseDetails.ProductID
      , Purchase.PurchaseDate       AS LastOfPurchaseDate 
FROM    dbo.Purchase 
INNER JOIN 
        dbo.PurchaseDetails 
ON dbo.Purchase.PurchaseID      = dbo.PurchaseDetails.PurchaseID
INNER JOIN 
        (
            SELECT  PurchaseDetails.ProductID
                  , GreatestPurchaseDate    = MAX(Purchase.PurchaseDate)
            FROM    dbo.Purchase 
            INNER JOIN 
                    dbo.PurchaseDetails 
            ON dbo.Purchase.PurchaseID      = dbo.PurchaseDetails.PurchaseID
            GROUP BY PurchaseDetails.ProductID
        ) AS LastPurchaseByProduct
ON  LastPurchaseByProduct.ProductID             = PurchaseDetails.ProductID
AND LastPurchaseByProduct.GreatestPurchaseDate  = PurchaseDetails.PurchaseDate
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On