Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT string Greater than using count()

Tags:

sql

count

I am trying to list groups that have more graduate than undergraduate student members. I feel I have the concept behind my idea, but making the query is a little more difficult then a simple translation. Below is my code, I currently am getting a missing right parenthesis error where COUNT(student.career = 'GRD'). Thanks.

SELECT studentgroup.name 
COUNT(student.career = 'GRD') - COUNT(student.career = 'UGRD') 
AS Gradnum FROM studentgroup 
INNER JOIN memberof ON studentgroup.GID = memberof.GroupID
INNER JOIN student ON memberof.StudentID = student.SID
WHERE Gradnum > 1;

1 Answers

SELECT studentgroup.GID, max(studentgroup.name)
FROM studentgroup 
INNER JOIN memberof ON studentgroup.GID = memberof.GroupID
INNER JOIN student ON memberof.StudentID = student.SID
GROUP BY studentgroup.GID
HAVING SUM(CASE WHEN student.career = 'GRD' THEN 1 
                WHEN student.career = 'UGRD'THEN -1
                ELSE 0
            END) >0
like image 71
valex Avatar answered Mar 15 '26 09:03

valex