Baguma Vincent
asked on
Counting occurences in a single field not the whole table/query
Is there a possible way of counting only the credits C of John only and not C in the whole table/query? Please help. Thank you a lot in advance!
can you please provide few rows and the expected output. Yes it is possible.
ASKER
Please check this -
SELECT COUNT(*) cnt
FROM yourtable
WHERE Name = 'JOHN'
SELECT COUNT(*) cnt
FROM yourtable
WHERE Name = 'JOHN'
or this-
SELECT COUNT(*) cnt
FROM yourtable
WHERE Name = 'JOHN' AND
( English like 'C%' or Maths like 'C%' or Literature like 'C%' or Biology like 'C%' )
SELECT COUNT(*) cnt
FROM yourtable
WHERE Name = 'JOHN' AND
( English like 'C%' or Maths like 'C%' or Literature like 'C%' or Biology like 'C%' )
@Baguma,
Are you trying to count the number of columns containing the letter "C", in this case 2?
Are you trying to count the number of columns containing the letter "C", in this case 2?
or may be this-
SELECT IIF( English like 'C%' , 1 , 0 ) + IIF( Maths like 'C%' , 1 , 0 ) + IIF( Literature like 'C%' , 1 , 0 ) + IIF( Biology like 'C%' , 1 , 0 )
FROM yourtable
WHERE Name = 'JOHN'
SELECT IIF( English like 'C%' , 1 , 0 ) + IIF( Maths like 'C%' , 1 , 0 ) + IIF( Literature like 'C%' , 1 , 0 ) + IIF( Biology like 'C%' , 1 , 0 )
FROM yourtable
WHERE Name = 'JOHN'
ASKER
Oh yes! The row is highlighted in my previous comment. John with D1 in English, C3 in Maths, D2 in Literature and C6 in Biology. Expected result is '2' showing the one in Maths and Biology.Thank you however
<<And this is a screenshot of the table and the total number of C i want to count of an individual/field/row>>
The reason your asking this question and the reason your having a problem in getting the answer with a SQL statement is that your table design is incorrect. What you have are repeating fields within a table, which is an indication that you have data that doesn't belong together.
What you have is three pieces of data:
Student
Subject
Grade
Thing is, the grade is related to the Subject (and even more specifically when they took that subject as they might take it multiple times if they fail). What you should have for a design is:
tblStudents - One record per Student
StudentID - Autonumber - Primary Key
FirstName
LastName
tblCourses - One record per course
CourseID - Autonumber - PK
CourseName
CourseDate
tblStudentCourses - One row per student/course combination
StudentCourseID - Autonumber - PK
StudentID - Long - CK1A
CourseID - Long - CK1B
Grade - Text
Now you could simply do a SQL COUNT() of every record for a specific StudentID in tblStudentCourses where the Grade was "C".
With your current design, you'll need to visit every row, and look through each of the fields. You'll also have a problem if you add another subject; you'll have to modify the design of the table to accommodate that.
What if a student doesn't take a course? When you see a NULL in the field, does that mean the grade is just missing or that they did not take the course?
What if you want to see their average grade over several years?
Those are some of the problems you'll have with the current design.
Take a step back and take the time to re-do it.
Jim.
The reason your asking this question and the reason your having a problem in getting the answer with a SQL statement is that your table design is incorrect. What you have are repeating fields within a table, which is an indication that you have data that doesn't belong together.
What you have is three pieces of data:
Student
Subject
Grade
Thing is, the grade is related to the Subject (and even more specifically when they took that subject as they might take it multiple times if they fail). What you should have for a design is:
tblStudents - One record per Student
StudentID - Autonumber - Primary Key
FirstName
LastName
tblCourses - One record per course
CourseID - Autonumber - PK
CourseName
CourseDate
tblStudentCourses - One row per student/course combination
StudentCourseID - Autonumber - PK
StudentID - Long - CK1A
CourseID - Long - CK1B
Grade - Text
Now you could simply do a SQL COUNT() of every record for a specific StudentID in tblStudentCourses where the Grade was "C".
With your current design, you'll need to visit every row, and look through each of the fields. You'll also have a problem if you add another subject; you'll have to modify the design of the table to accommodate that.
What if a student doesn't take a course? When you see a NULL in the field, does that mean the grade is just missing or that they did not take the course?
What if you want to see their average grade over several years?
Those are some of the problems you'll have with the current design.
Take a step back and take the time to re-do it.
Jim.
ASKER
@Dale, May be i post a full caption of my form and you get to know exactly whats causing hard times. I have unbound textboxes that are supposed to count grades of each student from various subjects. They are supposed to count all Dinstinctions of John from Maths, English, Literature and Biology. Then count Credits from the same subjects, Passes and failures all to be posted in respective unbound texboxes named D, C, P and F.
@Author - did u get the required from below-
SELECT IIF( English like 'C%' , 1 , 0 ) + IIF( Maths like 'C%' , 1 , 0 ) + IIF( Literature like 'C%' , 1 , 0 ) + IIF( Biology like 'C%' , 1 , 0 )
FROM yourtable
WHERE Name = 'JOHN'
SELECT IIF( English like 'C%' , 1 , 0 ) + IIF( Maths like 'C%' , 1 , 0 ) + IIF( Literature like 'C%' , 1 , 0 ) + IIF( Biology like 'C%' , 1 , 0 )
FROM yourtable
WHERE Name = 'JOHN'
Hi Baguma,
A feedback will be appreciated :)
Thnx
A feedback will be appreciated :)
Thnx
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.