473,387 Members | 1,722 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,387 software developers and data experts.

Simple SQL causing hair loss! :)

Hi,

Hoping someone can help with what should be a simple problem.

Have two tables:

Classes and Enroll

Classes contains some classes with a ClassID, Enroll has a ClassID and a MemberID.

When a member enrolls in a class a entry is made in the enroll table with their member ID and the class ID.

I want to be able to retrieve a list of classes with the number of people enrolled to the class, am using the following:

SELECT Class.Name, COUNT (*) AS EnrollCount
FROM Class left JOIN Enroll ON Enroll.ClassID = Class.ClassID
GROUP BY Class.Name

This is working except that classes with no members enrolled are still showing as one (I assume because the COUNT is counting the entry from the left join). If I use a inner join it simply does not show the classes with no members joined. I need a display of classes with number of members including those with no members.

Please help!
Jan 26 '10 #1
3 1698
ADezii
8,834 Expert 8TB
The following will give the correct results, but I'm sure that the SQL Gang will have a more efficient solution than mine (just drawing a blank on this one):
Expand|Select|Wrap|Line Numbers
  1. SELECT Class.Name, fClassCount([Class].[ClassID]) AS [Class Count]
  2. FROM Class LEFT JOIN Enroll ON Class.ClassID = Enroll.ClassID
  3. GROUP BY Class.Name, Class.ClassID;
Expand|Select|Wrap|Line Numbers
  1. Public Function fClassCount(lngClassID As Long)
  2.   fClassCount = DCount("*", "Enroll", "[ClassID] = " & lngClassID)
  3. End Function
Jan 27 '10 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi, and Welcome to Bytes!

As Adezii thought, there is a simple solution in SQL. The problem is the use of Count(*) which as you mention is simply counting the rows - so it returns a count of minimum 1 for each class name listed. This can be resolved simply by using Count(Class.ClassID) instead.

The SQL then just becomes

Expand|Select|Wrap|Line Numbers
  1. SELECT Class.Name, COUNT (Class.ClassID) AS EnrollCount
  2. FROM Class left JOIN Enroll ON Enroll.ClassID = Class.ClassID
  3. GROUP BY Class.Name
It works because the Count function in Access does not count nulls when given a specific field name as a qualifier, and the nulls in this case are where there are no Enroll table rows for that class as yet

-Stewart
Jan 27 '10 #3
ADezii
8,834 Expert 8TB
You didn't have to make it look that simple, Stewart! (LOL)
Jan 27 '10 #4

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

Similar topics

0
by: unixman | last post by:
As usual, it is 2:00am, and I'm pulling my hair out, finally resorting to posting in the newsgroups for help. :) Simple problem, in theory. Given table "map": CREATE TABLE map ( entry_id...
1
by: Tim Penhey | last post by:
I am currently refraining from tearing out any more hair. I have started at a new job and I am trying to get some quick tests for some COM components that are used here. This morning I...
1
by: D A H | last post by:
I have gotten the same exception in multiple projects. I have solved the underlying problem. My question is if anyone knew of a setting that would cause this exception to be thrown. A...
5
by: Eric | last post by:
Hi All, I'm very experienced in traditional ASP and am new to (am learning) ASP.NET. FYI: I am initially learning ASP.NET with VB.NET to ease the transition for me. I have encountered what I...
6
by: PJ | last post by:
I'm getting an error in the event viewer as well as a script error in the browser when a page references WebResource.axd. The application is running on W2k3 server and does not occurr on my local...
1
by: E.T. Grey | last post by:
I have been busting my nut over this for pretty much most of the day and it is driving me nuts. I posted this to an mySQL ng yesterday and I have not had any response (I'm pulling my hair out...
5
by: NoDBExperience | last post by:
I have a situation where i think my DB was hard coded?? Don't know, but i got this from talking to several people about my problem. Because this is the case, it is restricting my from changing or...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.