Solved

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

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Documents and settings folder 30 78
deduplicating based on criteria 2 21
Query Doesn't Allow New Records to be Added When Linked to SQL Server Backend 4 23
Filter a form 8 15
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

777 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