Solved

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

Posted on 2013-01-18
4
745 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 40

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

Technology Partners: 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!

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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 …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

685 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