简体   繁体   中英

SQL SELECT string Greater than using 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;
SELECT studentgroup.name 
SUM(CASE WHEN student.career = 'GRD' THEN 1 ELSE 0 END) - SUM(CASE WHEN student.career = 'UGRD' THEN 1 ELSE 0 END) 
AS Gradnum FROM studentgroup 
INNER JOIN memberof ON studentgroup.GID = memberof.GroupID
INNER JOIN student ON memberof.StudentID = student.SID
WHERE Gradnum > 1
GROUP BY studentgroup.name;
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

I have used WITH As clause which is supported by most of DBMS like SQL Server, PostGresSQL except MySQL

With grpTbl As
(

SELECT studentgroup.name As StudentGroupName,
       SUM( CASE WHEN student.career = 'GRD' THEN 1 ELSE 0 END ) AS 'TotalGraduate',
       SUM( CASE WHEN student.career = 'UGRD' THEN 1 ELSE 0 END ) AS 'TotalUnderGraduate'

FROM studentgroup 
INNER JOIN memberof ON studentgroup.GID = memberof.GroupID
INNER JOIN student ON memberof.StudentID = student.SID

)


SELECT StudentGroupName
FROM grpTbl 
WHERE TotalGraduate > TotalUnderGraduate

For MySQL you can use Temporary table to store resultset from First query and filter out the GroupNames which have more Graduate than UnderGraduate in WHERE clause . This method will work for other DBMS also difference being syntax of creating temporary table.

CREATE TEMPORARY TABLE grpTbl (
StudentGroupName varchar(255),
TotalGraduate INT,
TotalUnderGraduate INT
);


INSERT INTO grpTbl
SELECT studentgroup.name As StudentGroupName,
           SUM( CASE WHEN student.career = 'GRD' THEN 1 ELSE 0 END ) ,
           SUM( CASE WHEN student.career = 'UGRD' THEN 1 ELSE 0 END ) 

    FROM studentgroup 
    INNER JOIN memberof ON studentgroup.GID = memberof.GroupID
    INNER JOIN student ON memberof.StudentID = student.SID 


 SELECT StudentGroupName
    FROM grpTbl 
    WHERE TotalGraduate > TotalUnderGraduate


 DROP TABLE grpTbl 

One more option

SELECT studentgroup.name
FROM studentgroup INNER JOIN memberof ON studentgroup.GID = memberof.GroupID
                  INNER JOIN student ON memberof.StudentID = student.SID
GROUP BY studentgroup.name
HAVING COUNT(CASE WHEN student.career = 'GRD' THEN student.career END) 
         > COUNT(CASE WHEN student.career = 'UGRD' THEN student.career END)

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