Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Highest Values in Query

Hi All

I'm trying to create a query to get a list of purchase items that shows only the last time an item was purchased. The Table is a Purchase Item table where there are many duplicates.

I have a date field for each purchase (so the records are not duplicates really) I only want the ones with the latest date where there are duplicate discription, price and referance number.

How?
0
DatabaseDek
Asked:
DatabaseDek
  • 3
  • 2
1 Solution
 
mbizupCommented:
Use a grouping query:

SELECT ItemPurchased, Max(PurchaseDate) AS MostRecent
FROM YourTable
GROUP BY  ItemPurchased
0
 
Rey Obrero (Capricorn1)Commented:
select *
from [Purchase Item]
where [date] =(select max([date]) from [Purchase Item] as p where p.[referance number]=[Purchase Item].[referance number]

you can use an itemID or productid if there is one, in place of [reference number]
0
 
mbizupCommented:
Or if you want to include all those fields:


SELECT ItemPurchased, description, Price, RefNo, Max(PurchaseDate) AS MostRecent
FROM YourTable
GROUP BY  ItemPurchased, description, Price, RefNo
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
DatabaseDekAuthor Commented:
Just as a matter of interest is it possible to get the latest 2 records from the duplicates?.

Brilliantly simple. Thank you
0
 
mbizupCommented:
So the top two records for each 'group'?

That is definitely doable, but a little more complicated.

This page has a good explanation:
http://www.sql-ex.ru/help/select16.php

The first solution in that thread will work in Access SQL.

The second does not - I believe it is specific to SQL Server.
0
 
DatabaseDekAuthor Commented:
Thank you.That's interesting.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now