Solved

Second to Last Row

Posted on 2011-09-20
10
322 Views
Last Modified: 2012-05-12
Does Access have something similar to the Row Partition function in Oracle where you can rank or place a row number based on a column you want to order by?  For example, I have data in a table as listed below.  I can get the Max Order Date and the Min Order Date per AcctNumber easily but how would I get the second to last Order Date?  In this case it would be OrderID number 5 for AcctNumber 64.  OrderID is the primary key.  Also, not every Order ID will have a second to last order, (there could only be one).  In that case, I would still want the last and only Order ID returned.  Ideally the query results would look like this:

ACCTNUMBER    LAST_ORDER_DT    LAST_ORDER_ID    SECOND_TO_LAST_ORDER_DT  



ORDERID	ACCTNUMBER ORDERDATE
3	64	   9/2/2011 1:48 PM
4	64	   9/2/2011 2:19 PM
5	64	   9/2/2011 2:29 PM
6	64	   9/2/2011 2:30 PM

Open in new window

0
Comment
Question by:error_prone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 36570660
A simple self-join will do:

SELECT o1.AcctNumber, Max(o1.OrderDate) AS LastOrderDate, Max(o1.OrderID) AS LastOrderID, 
    Max(o2.OrderDate) AS SecToLastOrderDate
FROM tblOrders o1 LEFT JOIN 
    tblOrders o2 ON o1.OrderDate > o2.OrderDate AND o1.AcctNumber = o2.AcctNumber
GROUP BY o1.AcctNumber

Open in new window


Replace with your actual table/column names.  Note that Access will be unable to show that in the GUI query design view, but can show the SQL view.
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 250 total points
ID: 36570784
I think this works also ... and will show in the Query Design Grid:


SELECT TOP 1 o1.AcctNumber, o1.OrderDate, o1.OrderID, (SELECT TOP 1  o2.OrderDate
FROM tblOrders AS o2
WHERE o2.OrderID<o1.OrderID
ORDER BY o2.OrderID DESC) AS 2ndLastOD
FROM tblOrders AS o1
ORDER BY o1.OrderDate DESC;

AcctNumber      OrderDate                   OrderID      2ndLastOD
64                     09-02-2011 14:30:00      6             09-02-2011 14:29:00
0
 
LVL 75
ID: 36570789
And it covers this case also:

"Also, not every Order ID will have a second to last order, (there could only be one).  In that case, I would still want the last and only Order ID returned.  "

mx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36570833
Just to confirm, my suggestion covers the case of a sole order just splendidly :)
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36570843
MX,

Your query only works if there is but one AcctNumber in the table.  If there is >1 AcctNumber, you get the correct result for just one of the AcctNumber values.

:)

Patrick
0
 

Author Closing Comment

by:error_prone
ID: 36570844
Thanks!
0
 
LVL 75
ID: 36570845
Sorry ... I didn't mean to imply it did not.

But ... sorry bout not working in the grid :-)

mx
0
 
LVL 75
ID: 36570852
I might have missed that minor detail ...

mx
0
 
LVL 75
ID: 36572022
error_prone:
My solution does not cover part of what you want - showing results for each AcctNumber as Patrick noted, thus really is not a correct answer. So ... you should probably hit the Request Attention button :-)

mx
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36573462
Any Mods who may be reviewing this:

I see no need to alter the disposition here; if the Asker found MX's comment to be helpful, then a split is justified.  MX's comment is not necessarily wrong, it's just limited in its application.

:)

Patrick
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question