Solved

Access - query to get next to Last Record which is a group of records.

Posted on 2013-01-18
4
717 Views
Last Modified: 2013-02-02
Hello All
I struggle mightly with this getting the last record and now I need to get the one prior to the last record........
The field I have to select by is the ShiftCashCountID

My current code is
SELECT DISTINCTROW qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.ShiftCashCountID, qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.CurrencyDenominationsID, qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.[Sum Of CountAmt], qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.ShiftID, qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.BusinessDate
FROM qry_dta_ShiftCashCount_TtlByDenomALL_BusDay
GROUP BY qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.ShiftCashCountID, qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.CurrencyDenominationsID, qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.[Sum Of CountAmt], qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.ShiftID, qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.BusinessDate
ORDER BY qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.ShiftCashCountID, qry_dta_ShiftCashCount_TtlByDenomALL_BusDay.CurrencyDenominationsID;

Open in new window

0
Comment
Question by:wlwebb
  • 2
4 Comments
 
LVL 39

Expert Comment

by:als315
ID: 38795955
Do you need only one ShiftCashCountID or it should be found for groups?

Just FYI: You always will get answer quckly if you upload sample DB and show expected result, based on this sample.
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 38797401
i'm working from my iPad, so i'm going to make this short

To get the next to last record in a recordset you need to do something like

Select top 1 *
From (
Select top 2 * from yourtable
Order by sortfield DESC
) as temp
Order by sortfield ACS

The sub query sorts your data backwards, so the record you want to get is the 2nd record from that set.

The outer query resorts in your original sort order and takes the top 1 record, which is actually the 2nd to last record in your original result set.
0
 

Author Closing Comment

by:wlwebb
ID: 38847817
Thank you
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38847942
glad I could help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now