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

compare data

I've created two tables. One table (Classes) stores data about classes
that we offer. The Classes table stores the class id (classid) and the
max number of students allows (maxstudents). The other table
(Students) stores student data and the class they register for.

When a user registers for a class, the classid column data from Classes
populates the class column in Students.

I'm not sure how to count the number of students who registered for
course X, subtract that from the max number of students in the Classes
table, and display that the class if the max is reached either in a
warning dialog box or as text on the page.

I'm also populating a drop-down field on the registration form with the
class information from Classes. Confused yet?

I don't know much about SQL or .ASP. Any help is appreciated.

Jul 23 '05 #1
2 1168
> When a user registers for a class, the classid column data from
Classes
populates the class column in Students


That seems strange. Don't you want to allow a student to take more than
one class? I would have expected a joining table to implement a
many-to-many relationship:

CREATE TABLE StudentClasses (student_id INTEGER NOT NULL REFERENCES
Students (student_id), class_id INTEGER NOT NULL REFERENCES Classes
(class_id), PRIMARY KEY (student_id,class_id))

Try this for your query:

SELECT C.class_id,
C.maxstudents - COUNT(S.class_id)
FROM Classes AS C
LEFT JOIN Students AS S
ON C.class_id = S.class_id
GROUP BY C.class_id, C.maxstudents

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
Your design is wrong. Students do not have a class attribute; you need
enrollment for many classes.

CREATE TABLE Students
(stud_id INTEGER NOT NULL PRIMARY KEY,
...);

CREATE TABLE Classes
(class_id INTEGER NOT NULL PRIMARY KEY,
room_size INTEGER NOT NULL,
..);

CREATE TABLE Enrollment
(stud_id INTEGER NOT NULL,
class_id INTEGER NOT NULL,
PRIMARY KEY (stud_id, class_id),
...);
count the number of students who registered for course X, subtract

that from the max number of students in the Classes table,<<

SELECT E1.class_id,
(SELECT room_size
FROM Classes AS C1
WHERE C1.class_id = E1.class_id)
- COUNT(E1.student_id) AS available_seats
FROM Enrollment AS E1
GROUP BY E1.class_id;

Jul 23 '05 #3

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

Similar topics

1
by: Cameron | last post by:
Using SQL data compare i get the following error message: Could not allocate space for object '(SYSTEM table id: -701903460)' in database 'TEMPDB' because the 'DEFAULT' filegroup is full. The...
3
by: Stephen | last post by:
I have to write a .Net application which can compare SQL Databases including things like: - DB structure, PK's, FK's, indexes and types of indexes i.e. should be able to detect if the same index...
13
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte array to another? This is my current code //...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
8
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences...
0
by: TN Bella | last post by:
Hi, I am trying to get my compare validator to fire properly...Since I have panels the validator wouldn't work properly, the app would fire right but would insert the data regardless and the...
2
by: http://www.visual-basic-data-mining.net/forum | last post by:
Hi... Say i have this string call "data" in Form1, this string contains number "5" value.... how do i pass this string to the Form2 and compare with the combo box items... The combo box ...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
11
by: inpuarg | last post by:
I have 2 datatables. They are identical. I want to compare them by cell's content. They are all same. But dt1 == dt2 or dt1.GetHashCode() == dt2.GetHashCode() doesn 't work. There are big...
6
by: napatel04 | last post by:
Hi everyone, I would like to know if there is a quick query someone can help me write for the following scenario. I think I can do this with VBA but since this is suppose to be a temp. solution,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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...
0
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,...

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.