0

我必须得到我工作的地方学生的期末成绩。

该查询工作得很好,除了如果有一个 0 值,它包括它,这不会发生。如何从查询中排除 0,如果我没记错的话,排除的 0 必须来自查询中的 AVG。

这是我的查询:

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

我的查询包括一些存在但还没有值的评估中的 0,例如尚未应用的测验,尚未检查的课堂作业。

如何从考虑中删除这些值?是否有 AVGIFNOTNULL 或类似的东西?

4

2 回答 2

0
       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
于 2013-10-03T22:03:21.913 回答
0

我认为应该是这样的

将此添加到哪里

and AVG(gg.ReceivedPoints * ga.Weight / 100.0) <>0
于 2013-10-03T22:06:18.257 回答