user3290332
user3290332

Reputation:

Count total number of records

I have record of students, every day they undertakes a driving test =, I would like to calculate total number of students who have passed and failed their driving test for today's date e.g

StudentName  Status     date          pass     fail
test1        something    2014-05-14     1        0
test2        something    2014-05-14     0        1
test1        something    2014-04-14     0        1
test1        something    2014-03-14     0        1

At the moment I have this query - select count(case pass where 1 then 1 else null end) As TotalPassed, count(case pass where 1 then 1 else null end) as Totalfailed from DatabaseName where date>='2014-05-14' The problem is this query only search for total record from 2014-05-14, but what I want is total number of record for a specific student. For this query I get result as;

StudentName Status       date          pass      fail
test1       Something    2014-05-14     1          0
test2       something    2014-05-14     0          1

But what I need is the query to calculate the total number of time that each person have passed or failed his/her exams, so the expected result should be:

    StudentName  Status      date          pass      fail
    test1        something   2014-05-14     1          2
    test2        something   2014-05-14     0          1

Upvotes: 0

Views: 1027

Answers (4)

P. Camilleri
P. Camilleri

Reputation: 13218

You should use SUM instead of COUNT, and add a GROUP BY in the end.

SELECT StudentName, 
MAX(date) as date,
SUM(pass) As TotalPassed,
SUM(fail) As Totalfailed,
SUM(pass) + SUM(fail) as TotalTriesVersion1,
COUNT(*) as TotalTriesVersion2
FROM Table1 
GROUP BY StudentName

And here is the sqlfiddle (next time, please provide it yourself :) )

Bonus : I gave you two ways of computing the total number of time each student has taken the exam, as I understood from the comments that you wanted to have this too.

Upvotes: 3

OliC
OliC

Reputation: 175

try it with a group by

select StudentName, date, sumpass, fail, count(*) from table group by StudentName, date, pass, fail

Upvotes: 0

Indranil.Bharambe
Indranil.Bharambe

Reputation: 1498

select studentname , date , sum(pass) as Totalpass, sum(fail) as TotalFail from DatabaseName where date ='2014-05-14'
group by studentname,date

Upvotes: 0

Thanos Markou
Thanos Markou

Reputation: 2624

You need to use the GROUP BY Clause like this:

SELECT COUNT(CASE pass WHERE 1 THEN 1 ELSE NULL END) AS TotalPassed,
COUNT(CASE pass WHERE 1 THEN 1 ELSE NULL END) AS Totalfailed FROM DatabaseName
WHERE date>='2014-05-14' GROUP BY StudentName

Upvotes: 0

Related Questions