473,836 Members | 2,080 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 8570
Rabbit
12,516 Recognized Expert Moderator MVP
Then the syntax error you're getting shouldn't be the same. What's the new syntax error?
Feb 13 '12 #31
moishy101
46 New Member
The syntax error is on Count(qTT1.*) < 4
Feb 13 '12 #32
NeoPa
32,584 Recognized Expert Moderator MVP
Moishy101:
but I still get the syntax error on Count(qTT1.*) < 4 even after changing it to Count(*) < 4
Just the Count() problem now Rabbit. I'm just rushing out now, so if you can find my error before I get back you'll save me a task ;-)
Feb 13 '12 #33
NeoPa
32,584 Recognized Expert Moderator MVP
@Moishy101.
Actually, why don't you post all of your SQL, exactly as you have it now using Copy and Paste, and give us the names of all relevant objects if different from what I used in my suggestion. That way we can see if anything's amiss.
Feb 13 '12 #34
moishy101
46 New Member
Copy and paste (plus removing the formatting) is exactly what I did to your sql and table in post #23 (after the correction), so that post will show exactly what I have. Names of relevant objects are as you suggested (I don't ask not to listen!)
Feb 13 '12 #35
ADezii
8,834 Recognized Expert Expert
@moishy101:
I actually have a Hybrid (Query/VBA) Solution that you may/may not wish to utilize. I'll Post all of the relevant Details later on should you be interested. If you are not interested in this approach, and wish to go strictly SQL, simply let me know before then.
Feb 13 '12 #36
moishy101
46 New Member
@ADezii:

I'm always open to all options, there's more than one way to skin a cat;-)

I actually would like to see it, I believe in broadening my horizons.
Feb 13 '12 #37
ADezii
8,834 Recognized Expert Expert
Using NeoPa's Post# 23 as a foundation, I created a simple Query that will accomplish the Task, and produce the desired results. The Query consists of only 2 Fields: (UNIQUE [ClientID]s) ASC and a Calculated Field (TOP_3_Per_Clie nt:) fCalcTOP3([ClientID]) that passes the [ClientID] to a Public Function. This Function (fCalcTOP3) contains all the Logic, and returns a Delimited String consisting of the Transaction Numbers and Hebrew Months for the TOP 3 Months sorted ASC by Month. I'll Post the SQL, Function Definition, and the Results based on my Sample Data. To further simplify matters, I've attached the Demo Database that I used for this Thread.

The obvious Disadvantages are:
  1. Because of the JOIN involved, the actual Hebrew Month Names must match exactly with those in tblHebrewMonthO rder, or they will not be included in the Analysis. A couple of simple, Update Queries, can easily fix this problem so that all Months conform.
  2. Processing Time may be significant for a large Data set, which you have indicated is the case.
Advantage:
  1. OUTPUT can easily be converted to a number of Formats, namely: a Results Table, Delimited String in a Query, Debug Window, Text File, etc.
  1. SQL:
    Expand|Select|Wrap|Line Numbers
    1. SELECT DISTINCT tblTransaction.ClientID, fCalcTOP3([ClientID]) AS TOP_3_Per_Client
    2. FROM tblTransaction
    3. GROUP BY tblTransaction.ClientID
    4. ORDER BY tblTransaction.ClientID;
    5.  
  2. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcTOP3(bytClientID As Byte)
    2. Dim MyDB As DAO.Database
    3. Dim rst As DAO.Recordset
    4. Dim strSQL As String
    5. Dim strBuild As String
    6.  
    7. strBuild = ""       'INITIALIZE
    8.  
    9. 'Are there at least 3 Records for the Passed ClientID
    10. If DCount("*", "tblTransaction", "[ClientID] = " & bytClientID) < 3 Then
    11.   fCalcTOP3 = "N/A"
    12.     Exit Function
    13. End If
    14.  
    15. strSQL = "SELECT tblTransaction.ClientID, tblTransaction.TransactionID, tblHebrewMonthOrder.MonthName, " & _
    16.          "tblHebrewMonthOrder.Priority FROM tblHebrewMonthOrder INNER JOIN tblTransaction ON " & _
    17.          "tblHebrewMonthOrder.MonthName = tblTransaction.TransactionMonth WHERE tblTransaction.ClientID = " & _
    18.           bytClientID & " ORDER BY tblTransaction.ClientID,tblHebrewMonthOrder.Priority;"
    19.  
    20. Set MyDB = CurrentDb
    21. Set rst = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
    22.  
    23. rst.MoveLast: rst.MoveFirst
    24.  
    25. 'The TOP 3 for this ClientID will actually be the Last 3 Records, Move to 3rd from Bottom
    26. rst.Move rst.RecordCount - 3        'TOP of the TOP 3
    27.  
    28. Do While Not rst.EOF
    29.   strBuild = strBuild & rst!TransactionID & "," & rst!MonthName & " | "
    30.     rst.MoveNext
    31. Loop
    32.  
    33. fCalcTOP3 = Left$(strBuild, Len(strBuild) - 3)
    34.  
    35. rst.Close
    36. Set rst = Nothing
    37. End Function
  3. Query OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. ClientID    TOP_3_Per_Client
    2. 1           9870561,Adar I | 5374530,Nissan | 2323456,Tammuz
    3. 2           5376892,Nissan | 5327891,Sivan | 6423568,Tammuz
    4. 3           N/A
    5. 4           N/A
    6. 5           991254,Cheshvan | 345678,Sivan | 987654,Av
    7.  
OOPs!: Code Line# 23 can be removed from the Attachment, since it was there for testing purposes only.
Attached Files
File Type: zip Hebrew Months.zip (24.8 KB, 118 views)
Feb 13 '12 #38
moishy101
46 New Member
@ADezii:

I greatly appreciate your help. There seems to be one problem, if a client has less than 3 records it won't show up (N/A), is it possible if there are more than 0 records and less than 3 records, to show the records and nothing where empty?
Feb 13 '12 #39
ADezii
8,834 Recognized Expert Expert
moishy101:
is it possible if there are more than 0 records and less than 3 records, to show the records and nothing where empty?
I can build that into the Logic and get to you with the Revisions. Special conditions such as these should have been stated earlier, especially since it effects the gentlemen working on the SQL approach. The changes to the Code are relatively easy, not so for the SQL involved.
Feb 13 '12 #40

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

Similar topics

1
1572
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...
10
3742
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...
5
1440
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 ***
2
15288
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 )
4
5150
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
2
1475
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
1
2265
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:...
4
1488
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 ...
4
4897
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()
5
1942
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
0
9657
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,...
0
10532
jinu1996
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...
1
10577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
tracyyun
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...
0
9359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7774
isladogs
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...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
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 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.