简体   繁体   中英

Exclude 0 Values from the query

I have to get the final grades of the students of the place where I work.

The query works great except that if there's a 0 value, it includes it, and this cannot happen. How can I exclude 0 from my query, if I'm not mistaken, the 0 excluded must be from the AVG in the query.

Here is my query:

SELECT 
gbc.StudentID as [Student ID],
per.FirstName + ' ' + per.MiddleName + ' ' + per.LastName as [Student Name], 
gbc.ClassID as [Class ID],
cls.Name as [Class Name],
ROUND(SUM(gbc.weightedgrade),0) as [Final Grade]
FROM (SELECT 
    gg.ClassID, 
    gg.StudentID, 
    ga.AssessmentId, 
    ga.Title , 
    AVG(gg.ReceivedPoints * ga.Weight / 100.0) as weightedgrade
FROM GbkAssessments as ga INNER JOIN GbkGrades as gg ON ga.AssessmentID = gg.AssessmentID AND ga.ClassID = gg.ClassID 
JOIN Classes as cls on ga.classID = cls.classID JOIN GbkSummary as gbs on ga.classID = gbs.classID
WHERE gg.StudentID = 1201417 and cls.YearID = 251 and gbs.TermID = 1
GROUP BY gg.StudentID, gg.ClassID, ga.AssessmentId, ga.Title) as gbc JOIN Classes as cls on gbc.classID = cls.classID 
join Person as Per on Per.PersonID = gbc.StudentID join Person_Student as PS on Per.PersonID = PS.StudentID 
GROUP BY gbc.StudentID, gbc.ClassID, cls.Name, per.FirstName, per.LastName, per.MiddleName
ORDER BY gbc.ClassID ASC

My query includes the 0's from some assessments that are present but don't have a value yet, example a quiz that hasn't been applied, a classwork that hasn't been checked.

How can I remove those values from being taken on account? Is there an AVGIFNOTNULL or something like that?

       AVG (CASE WHEN Value <> 0 THEN Value ELSE NULL END)

        SELECT 
        gbc.StudentID as [Student ID],
        per.FirstName + ' ' + per.MiddleName + ' ' + per.LastName as [Student Name], 
        gbc.ClassID as [Class ID],
        cls.Name as [Class Name],
        CASE WHEN gbc.weightedgrade IS NOT NULL THEN ROUND(SUM(gbc.weightedgrade),0) ELSE NULL END as [Final Grade]
        FROM (SELECT 
            gg.ClassID, 
            gg.StudentID, 
            ga.AssessmentId, 
            ga.Title , 
            CASE WHEN gg.ReceivedPoints IS NOT NULL AND ga.Weight IS NOT NULL  
              THEN
                 AVG(gg.ReceivedPoints * ga.Weight / 100.0) 
                ELSE NULL 
              END as weightedgrade
        FROM GbkAssessments as ga INNER JOIN GbkGrades as gg ON ga.AssessmentID = gg.AssessmentID AND ga.ClassID = gg.ClassID 
        JOIN Classes as cls on ga.classID = cls.classID JOIN GbkSummary as gbs on ga.classID = gbs.classID
        WHERE gg.StudentID = 1201417 and cls.YearID = 251 and gbs.TermID = 1
        GROUP BY gg.StudentID, gg.ClassID, ga.AssessmentId, ga.Title) as gbc JOIN Classes as cls on gbc.classID = cls.classID 
        join Person as Per on Per.PersonID = gbc.StudentID join Person_Student as PS on Per.PersonID = PS.StudentID 
        GROUP BY gbc.StudentID, gbc.ClassID, cls.Name, per.FirstName, per.LastName, per.MiddleName
        ORDER BY gbc.ClassID ASC

I think it should be like this

add this to where

and AVG(gg.ReceivedPoints * ga.Weight / 100.0) <>0

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