简体   繁体   中英

How to COUNT values and and use it as a variable in a Stored Procedure

Basically, What i am trying to do is ;

The number of classes required is a variable. EG. The user could put 5 , 4 as a minimum.

Since i am new, i cant upload pictures, But here is a picture of my ER diagram, to get a idea of the table structures.

I am using sql server 2005

First off your question is poorly phrased; it is difficult to get a sense of what it is you are trying to achieve.

To get the number of rows from a dataset which meet the required condition, then use SELECT COUNT(0) FROM Tablename Where WhereCondition.

I can't see your ER diagram so I'll hypothesise; this should return attendance by class.

DECLARE @Students TABLE (id int, StudentName nvarchar(max));
DECLARE @Classes TABLE (id int, ClassName nvarchar(max))
DECLARE @StudentClassAttendance TABLE (ClassAttendanceID int, StudentId int, ClassId int, StartTime datetime)

SELECT sca.StudentID, s.StudentName, COUNT(sca.ClassAttendanceID), c.ClassName
FROM 
@Students s
INNER JOIN @StudentClassAttendance sca
ON sca.StudentId = s.Id
INNER JOIN @Classes c
ON c.Id = sca.ClassId
GROUP BY 
sca.StudentID, s.StudentName, ClassName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM