My Assistant
![]() ![]() |
|
|
Mar 6 2012, 08:00 AM
Post
#1
|
|
|
New Member Posts: 17 |
I'm new to access and i'm trying to use a query to return the 2 most recent records (based on date and time) for a group of products and their price changes. Data looks like this:
ProdID Date of change Time of Change Price UP 3/5/12 18:00 2.25 UP 3/5/12 13:00 2.37 UP 3/4/12 18:00 2.68 UP 3/3/12 18:00 2.45 UD 3/5/12 18:00 2.15 UD 3/4/12 18:00 2.58 UD 3/3/12 18:00 2.25 UD 3/1/12 18:00 2.69 I want to return the 3/5/12 18:00 and 3/5/12 13:00 price for UP and and 3/5/12 18:00 and 3/4/12 18:00 record for UD |
|
|
|
Mar 6 2012, 08:29 AM
Post
#2
|
|
|
UtterAccess Ruler Posts: 1,090 |
CODE (SELECT Top 2 [ProdID], [Date of Change], [Time of Change], [Price]
FROM YourTableName WHERE (((ProdId) = "UP")) ORDER BY [Date Of Change] DESC, [Time of Change] DESC) UNION (SELECT Top 2 [ProdID], [Date of Change], [Time of Change], [Price] FROM YourTableName WHERE (((ProdId) = "UD")) ORDER BY [Date Of Change] DESC, [Time of Change] DESC) ORDER BY [ProdID], [Date of Change] DESC, [time Of Change] DESC; This post has been edited by arnelgp: Mar 6 2012, 08:53 AM |
|
|
|
Mar 6 2012, 08:34 AM
Post
#3
|
|
|
UtterAccess Editor Posts: 13,753 From: England (North East / South Yorks) |
You might not realize it - but you've already made a really good start in the question itself.
You've identified that you need some criteria by which to identify the order of the rows. (So often the "most recent" is offered without qualification or order of entry is assumed, without any data assistance - which it can't be.) What you want is a TOP N per group query. http://allenbrowne.com/subquery-01.html#TopN If that doesn't give you enough of a pointer, then shout. Cheers. |
|
|
|
Mar 6 2012, 08:40 AM
Post
#4
|
|
|
New Member Posts: 17 |
Thank you so much for your help. What if i don't want to explicitly specify each product in the code, but rather return the top 2 for every product in the table
|
|
|
|
Mar 6 2012, 08:47 AM
Post
#5
|
|
|
Utter Access VIP Posts: 1,007 |
SELECT D.ProdID, D.[Date of change], D.[Time of Change], D.Price
FROM Dhill23 As D WHERE ([Date of change]+[Time of Change]) In (SELECT Top 2 [Date of change]+[Time of Change] AS Expr1 FROM Dhill23 WHERE Dhill23.ProdID = D.ProdID) Order by D.ProdID, [Date of change]+[Time of Change] DESC; Change all the Dhill23's to the actual table name. NOTE: If either the Date of change or Time of Change fields are null, that record won't be returned. |
|
|
|
Mar 6 2012, 09:27 AM
Post
#6
|
|
|
UtterAccess Ruler Posts: 1,090 |
Revised the above code from Mr. Whittle.
CODE SELECT D.ProdID, D.[Date of change], D.[Time of Change], D.Price
FROM YourTable As D WHERE ([Date of change]+[Time of Change]) In (SELECT Top 2 [Date of change]+[Time of Change] AS Expr1 FROM YourTable WHERE YourTable.ProdID = D.ProdID ORDER BY [Date Of Change] Desc, [Time Of Change] Desc) Order by D.ProdID, D.[Date of change] DESC, D.[Time of Change] DESC; |
|
|
|
Mar 6 2012, 09:46 AM
Post
#7
|
|
|
Utter Access VIP Posts: 1,007 |
arnelgp,
Good catch. Thanks! |
|
|
|
Mar 6 2012, 10:05 AM
Post
#8
|
|
|
UtterAccess Ruler Posts: 1,090 |
Mr. Whittle I am keeping your code with me. I learned a lot from this one (IMG:style_emoticons/default/notworthy.gif)
|
|
|
|
Mar 6 2012, 12:05 PM
Post
#9
|
|
|
New Member Posts: 17 |
Thank you both.
|
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 25th May 2013 - 10:22 AM |