query to find most recent price change

MrHaggis

New Member
Joined
Oct 6, 2006
Messages
18
Hi all,
I've searched for what I think is probably a simple answer, but so far I'm lost...

I have a table with project, model and product information. Every time the price of a product changes, it's recorded in this table, so for every product, I could have many lines:

Project,Model,Product,Date,Price

TUC,Willow,Countertop,01/01/06,$40.00
TUC,Willow,Countertop,01/10/06,$35.00
TUC,Willow,Cabinet,02/01/06,$420.00
TUC,Willow,Cabinet,03/05/06,$400.00
TUC,Willow,Countertop,06/01/06,$41.00

I want to run a query to extract only the most current price for every product. I can get as far as creating a query that includes all fields except Price and use MAX on the date field, but how do I make the query include the price that goes with the most recent date? If I've formatted this post correctly, the records that would be returned in this example should be in bold.

Thanks in advance.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi

You could do this using an SQL subquery (if you want just the one query) but I find the easiest & quickest way of doing this is with 2 queries. Use your first query to find the last date (using the max function as you have) for each product (and project and model if required). You probably already have this query set up.

Create a second query that is based on the first query and the original table. Create links (by clicking and dragging) the fields from your first query to the corresponding fields on the table. Then in your query grid include all of the the fields from the first query and the price from the table. The output from this query should be what you want.

HTH, Andrew
 
Upvote 0
Thanks Andrew - that's the direction I was heading in, but didn't know if it was possible to get fancy with SQL to do it all in one statement. Your reply is appreciated.
Cheers,
H
 
Upvote 0
You can do this with a sub-query:
Code:
select
Project
,Model
,Product
,Date
,Price 
from someTable as X
where 
[date] = (
               select max([date])
               from someTable 
               where 
               Project = x.Project 
               AND Model = x.Model
               AND Product = x.Product
         );

hth,
Giacomo
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top