UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

> Return First And Second Most Recent Records    
 
   
dhill23
post 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
Go to the top of the page
 
+
 
Start new topic
Replies (1 - 8)
arnelgp
post 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
Go to the top of the page
 
+
LPurvis
post 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.
Go to the top of the page
 
+
dhill23
post 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
Go to the top of the page
 
+
Jerry Whittle
post Mar 6 2012, 08:47 AM
Post #5

Utter Access VIP
Posts: 1,004



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.
Go to the top of the page
 
+
arnelgp
post 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;
Go to the top of the page
 
+
Jerry Whittle
post Mar 6 2012, 09:46 AM
Post #7

Utter Access VIP
Posts: 1,004



arnelgp,

Good catch. Thanks!
Go to the top of the page
 
+
arnelgp
post 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)
Go to the top of the page
 
+
dhill23
post Mar 6 2012, 12:05 PM
Post #9

New Member
Posts: 17



Thank you both.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 22nd May 2013 - 04:25 PM