473,855 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Last 3 records by group

46 New Member
How can I retrieve the last 3 records from the following table. The table has the following fields and sample data:

ClientID TransactionID TransactionDate

1 6511216 01/02/2012
1 5332573 18/05/2011
1 9849528 11/02/2012
1 5374530 24/08/2010
1 5711675 26/04/2009
1 4001184 01/09/2011
2 9087526 15/07/2011
2 6524824 06/08/2009
2 5376892 26/07/2011
2 5327891 28/10/2008
2 6423568 11/11/2011
2 5379827 16/06/2012

the results should be (order by TransactionDate ):

ClientID TransactionID TransactionDate

1 4001184 01/09/2011
1 6511216 01/02/2012
1 9849528 11/02/2012
2 5376892 26/07/2011
2 6423568 11/11/2011
2 5379827 16/06/2012

Note: I can't use 'Select Top 3' a. because of the month format b. this must work for a table where TransactionDate is a different field with text data.
Feb 11 '12
69 8582
32,584 Recognized Expert Moderator MVP
It would indeed. I would consider that as fitting within "the data to be defined for use". However, as a solution for this problem, it's not a very strong one, as it involves the data for each record being processed through a function. Fine for small numbers of records, but you say the numbers are large in this case, so I would advise use of a separate table to manage the ordering for you.
Feb 12 '12 #21
12,516 Recognized Expert Moderator MVP
Try something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT T1.groupField, T1.orderField, T1.otherField
  2. FROM someTable T1,
  3.    someTable T2
  4. ON T1.groupField = T2.groupField
  5.    AND T1.orderField < T2.orderField
  6. GROUP BY T1.groupField, T1.orderField, T1.otherField
  7. HAVING COUNT(*) < 3
Feb 13 '12 #22
32,584 Recognized Expert Moderator MVP
Assuming we start with your table (for convenience we'll refer to it as [tblTransaction] unless/until we hear otherwise) and a new table called [tblHebrewMonthO rder] with the following setup and data :
Table Name = [tblHebrewMonthO rder]
Expand|Select|Wrap|Line Numbers
  1. HebrewMonth  MonthOrder
  2. Tishrei              1
  3. Cheshvan             2
  4. Kislev               3
  5. Tevet                4
  6. Shvat                5
  7. Adar I               6
  8. Adar II              7
  9. Nissan               8
  10. Iyar                 9
  11. Sivan               10
  12. Tammuz              11
  13. Av                  12
  14. Elul                13
Next we have a query called [qryTranData] which is simply a query that links the data in [tblTransaction] with that in [tblHebrewMonthO rder] and shows the order required :
Query Name = [qryTransactionH ebrew]
Expand|Select|Wrap|Line Numbers
  1. SELECT   tT.ClientID
  2.        , tT.TransactionNumber
  3.        , tHMO.MonthOrder
  4. FROM     [tblTransaction] AS [tT]
  5.          INNER JOIN
  6.          [tblHebrewMonthOrder] AS [tHMO]
  7.   ON     tT.TransactionHMonth = tHMO.HebrewMonth
  8. ORDER BY tT.ClientID ASC
  9.        , tHMO.MonthOrder DESC
We now have a dataset which contains the requisite information as well as being sorted in the correct order (which will be ignored when we use it again in [qryTransactionT op], but may be helpful to you for viewing the data and understanding what is what).

Query Name = [qryTransactionT op]
Expand|Select|Wrap|Line Numbers
  1. SELECT   qTT1.*
  2. FROM     [qryTransactionTop] AS [qTT1]
  3.          INNER JOIN
  4.          [qryTransactionTop] AS [qTT2]
  5.   ON     (qTT1.ClientID = qTT2.ClientID)
  6.  AND     (qTT1.MonthOrder >= qTT2.MonthOrder)
  7. GROUP BY qTT1.ClientID
  8.        , qTT1.TransactionNumber
  9.        , qTT1.MonthOrder
  10. HAVING   Count(qTT1.*) < 4
  11. ORDER BY qTT1.ClientID ASC
  12.        , qTT1.MonthOrder DESC
The idea at this point is to link the query to itself on [ClientID] and [MonthOrder] and treat each record of the first query as a GROUP. With the AND line (#6) limiting which records of the second query are included, the HAVING clause can pick out only those GROUPs where the number of records is up to three.
Feb 13 '12 #23
46 New Member

I tried what you suggested and I got some errors.
In qryTransactionT op there is a syntax error on Count(qTT1.*) < 4
In qryTransactionH ebrew I get a Enter Parameter Value on tHMO.Order
Feb 13 '12 #24
32,584 Recognized Expert Moderator MVP
Have you set up the table [tblHebrewMonthO rder] as illustrated on your system?
Feb 13 '12 #25
46 New Member
Yes I have.
Thanks for your help.
Feb 13 '12 #26
32,584 Recognized Expert Moderator MVP
Found it. My mistake. I changed the design half way through and missed updating one of the references. I've updated it no so you can try it again if you re-copy/paste the contents of [tblHebrewMonthO rder].

PS. Let me know if replacing qTT1.* with simply * in line #10 works for [qryTransactionT op] :
Expand|Select|Wrap|Line Numbers
  1. HAVING   Count(*) < 4
Feb 13 '12 #27
46 New Member
Thank you, as you know you've successfully changed the qryTransactionH ebrew, but I still get the syntax error on Count(qTT1.*) < 4 even after changing it to Count(*) < 4
Feb 13 '12 #28
12,516 Recognized Expert Moderator MVP
Where is the tHMO.Order that you mentioned in your error? I don't see it in NeoPa's code so it shouldn't be in your code either.
Feb 13 '12 #29
46 New Member

NeoPa corrected the query as you can see from the last edited on post #23.
Feb 13 '12 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Matt | last post by:
I have a project where I need to have navigation buttons. Now I got them working but I found a problem that occurs when there are records deleted from the DB. The DB I am accessing has an ID column that is the primary key and uses AutoNumber. My program also allows the user to delete records. My problem occurs when someone deletes a record other than the first or last record in the DB. The way my Next navigation button works is that it...
by: Lyn | last post by:
I have a form set to Single Form mode with which I can cycle through the records in a table via Next and Previous buttons. To avoid users pressing the Previous button on the first record and the Next button on the last record, I would like to disable one or both buttons when the first and/or last record is displayed. I am not sure how to do this when the RecordSource is simply the table. I know that if the record source were a...
by: booksnore | last post by:
I am reading some very large files greater than 10 GB. Some of the files (not all) contain a header and footer record identified by "***" in the first three characters of the record. I need to delete the header or footer record before reading the file into a database. Whats the best way to do this in C#? Any help appreciated. Joe *** Sent via Developersdex http://www.developersdex.com ***
by: schapopa | last post by:
Hi, I want to create query where I could group records by quarters, and get the last record in each group. e.g Create Table MyTable ( Value , date )
by: Tomas | last post by:
I'm creating MS Access database and I need to have in query an average of 3 last records. How to do it? Maybe here is some function or sql expresion? Thanks
by: perryche | last post by:
I don't know where to begin search for this... I want to add a record, but still able to see the page, without bringing it to a new page and have to scroll back to see other records. I hope I explain myself here. Or, is there a way to add record being on the top of other records and not go to the bottom? Thanks. Perry
by: jith87 | last post by:
hi, i am importing a text file into oracle database using sql loader.i need to ommit the first and last records of the text file while importing... can anybody help???? this is my text file... A: Removable 0.0% C: Fixed NTFS 39.06 GB 26.10 GB 66.8% D: Fixed NTFS 43.95 GB 30.44 GB 69.3% E:...
by: PeteM | last post by:
Using SQL Server 2005 I have on a number of occasions, succesfully used the following code to create tables with the last dated results or observations for each Patient. However I am now being asked to do the same in Access 2003 and all the variations I can think of return the error message “At most one record can be returned by this subquery”. Do I have to give up with Access? SELECT Patient, (SELECT TOP 1 t1.LabResult1 FROM ...
by: Greg (codepug | last post by:
I want to display the last 22 records in my continuous form. I have writen the following code and it works, but was wondering if there were any better suggestions for accomplishing this. My code scrolls the screen as it goes to bottom and than scrolls out the 22 recs. My code: Private Sub GoToBottom()
by: asdasd10 | last post by:
i tired do it alone but its gives me an eror like that: "r object cant move back" here is my code : <% path = Server.mappath("../db/ServerData.mdb") set con = Server.createobject("ADODB.Connection") con.open "driver={Microsoft Access Driver (*.mdb)};DBQ="&path set r = con.execute("select * from News") Dim i
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.