473,673 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Count in a query - this must be simple!

Hi

I have the following problem, which I am sure is easy to resolve for
many Access users.

A table contains the following fields:
1) Student_Id
2) Results_Id

Problem : I wish to create a select query that shows both fields, is
sorted by field one with a counter that is reset when Student_Id
changes.

Example
Student_Id Results_Id Counter
100 1222 1
100 1509 2
100 1604 3
101 1003 1
101 1093 2
102 2039 1
103 1039 1
103 3939 2

.....and so on.

Your help would be appreciated.

David
Nov 13 '05 #1
2 1448

"David Gill" <d.****@shu.ac. uk> wrote in message
news:67******** *************** **@posting.goog le.com...
Hi

I have the following problem, which I am sure is easy to resolve for
many Access users.

A table contains the following fields:
1) Student_Id
2) Results_Id

Problem : I wish to create a select query that shows both fields, is
sorted by field one with a counter that is reset when Student_Id
changes.

Example
Student_Id Results_Id Counter
100 1222 1
100 1509 2
100 1604 3
101 1003 1
101 1093 2
102 2039 1
103 1039 1
103 3939 2

....and so on.

Your help would be appreciated.

David

Insert a new module and paste the following function in so the complete
module looks is as below. Make sure it compiles (Debug=>Compile ) and then
save the module.

' --------------------------------------------------------------------
Option Compare Database
Option Explicit

Public g_lngStudentID As Long
'
Public g_lngCount As Long
'

Public Function RowNumber(lngSt udentID As Long) As Long

If lngStudentID = g_lngStudentID Then
g_lngCount = g_lngCount + 1
Else
g_lngStudentID = lngStudentID
g_lngCount = 1
End If

RowNumber = g_lngCount

End Function
' --------------------------------------------------------------------
Your query could then be:

SELECT Student_Id,Resu lts_Id, RowNumber([Student_Id]) AS RowNumber
FROM tblStudentResul ts
ORDER BY Student_Id, Results_Id
The function as posted has no error-handling and must be passed a Student_Id
which is a long integer. If there is the possibilty of a blank (null) value
in this column then you could either amend the function or use something
like RowNumber(Nz([Student_Id], 0)) to change the nulls to zeroes.
Nov 13 '05 #2
"David Gill" <d.****@shu.ac. uk> wrote in message
news:67******** *************** **@posting.goog le.com...
Hi

I have the following problem, which I am sure is easy to resolve for
many Access users.

A table contains the following fields:
1) Student_Id
2) Results_Id

Problem : I wish to create a select query that shows both fields, is
sorted by field one with a counter that is reset when Student_Id
changes.

Example
Student_Id Results_Id Counter
100 1222 1
100 1509 2
100 1604 3
101 1003 1
101 1093 2
102 2039 1
103 1039 1
103 3939 2

select d.Student_Id, d.Results_Id,
(
select count(*) from david as d2
where d2.Student_Id = d.Student_Id
and d2.Results_Id <= d.Results_Id
) as counter
from david as d
order by d.Student_Id, d.Results_Id
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4201
by: Mark | last post by:
Hi, I have a Access database with two tables; a category table and an images table. In the Category table I have a field for Index number and a field for Categories. The images table has a field for all the images and a field to specify what category the images are assigned to. I am trying to write a simple Select query that will return a list of Categories from my Category table, and in the second colum a count of the number of images...
2
1596
by: Gregory.Spencer | last post by:
Help, I have a query in MySQL which gets the details of members of a club. e.g. Select * from members. however, in the same query I want to return the amount of "functions" the member has attended. therefore there is a "functions" table and because functions has a
3
9106
by: C. Homey | last post by:
I have an Access 2000 database (42,000+ records) for which I need a count of records by state (AL=320, AK=92, NY=1,932, etc). Obviously, I don't want to (and probably can't...) do the count manually. I know there is a Count function, but I don't know how to make it work. Can anyone help me, please? As an aside, I tried to export the data to an Excel spreadsheet, but it will NOT save as a new file. I have no idea why that is the case...
3
5020
by: Dagpauk | last post by:
Assume the following table holding information about the planning date and execution date for an imaginary "objects" ObjectPlan ObjectID PlannedDate ExecutedDate 1 19.03.04 28.03.04 2 19.03.04 24.03.04 3 19.03.04 19.04.04 1 19.04.04 20.04.04
1
1535
by: Simon Matthews | last post by:
Hope someone can help an Access beginner! I've just started keeping my surgical logbook on access and it's a simple flat-file affair. I have created several queries that will list cases performed at different hospitals and reports based on the queries to print out the relavent details. What I would like to do is have a summary sheet in the Report Footer section that lists a grid of each type of procedure performed as well as the...
6
11493
by: Nicolae Fieraru | last post by:
Hi All, I have a query, Select Count(BoolField) from tblMyTable, Where BoolField = true. If I run the query by itself, it returns the number of true records I want to use the result of that query in my VBA code, like this: If (result of the query > 0) then do something
1
3179
by: James | last post by:
Access 2003, trying to count the number of records that meet a criteria. According to Help: "In the Database window, click Queries under Objects, and then click New on the database window toolbar. Add the table, view, or function you want to summarize in the Diagram pane. " OK, that's easy.....but then..... "Right-click the background of the Diagram pane, then choose Group By
3
2807
by: amanda | last post by:
Hope someone can help me with this - I've been staring at it stupidly for hours now, convinced there must be an easy way to achieve the results I want: I have a very large table recording every sale made by one of 90 salespeople (2 years of data), and the date the sale was made. I need a report that shows a list of the salespeople down the left hand side and then for each month the total number of sales and the total value of sales. I...
9
10838
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using LDIFDE as a comparison I get the same results. No members means just that, an empty group. Zero means that the DirectorySearcher.SizeLimit has been exceeded....
8
4724
by: Thomas | last post by:
Hi! I´m a newbie with this and I´m trying to build a forum of my own but have stumbled on my first problem as early as the opening page. To the problem: I want to show a simple forum layout with tables looking something like this Forum Name | Topics | Posts | Last Post
0
8508
marktang
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...
0
8953
Oralloy
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...
1
8652
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
7484
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
6264
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
5727
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
4448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2849
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
3
1851
bsmnconsultancy
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.