473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1442

"David Gill" <d.****@shu.ac.uk> wrote in message
news:67*************************@posting.google.co m...
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(lngStudentID 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,Results_Id, RowNumber([Student_Id]) AS RowNumber
FROM tblStudentResults
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.google.co m...
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
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...
2
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...
3
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...
3
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...
1
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...
6
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...
1
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...
3
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...
9
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...
8
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.