Solved

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

Posted on 2013-01-18
4
708 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
glad I could help.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now