汇总列中的总值时,SQL查询未返回期望值

问题描述

我有这个模式:

  • Students(sid:int,firstName:str,lastName:str,yearStarted:int)

  • Majors(sid:int,major:str)
    注意:一个学生可能有多个专业。

  • Grades(sid:int,cid:int,积分:int,成绩:int)
    注意:sid,cid =外键,等级:A = 4,B = 3,C = 2,D = 1,F = 0。

  • Courses(cid:整数,数字:int,教授:str,专业:str,年份:int,学期:str) 注意:cid在整个学期中是唯一的。学期是夏季,秋季或春季。如果两个课程的课程号+专业相同,则它们是相同的

在这种模式下,我需要“提供SQL查询,该查询生成每个学生的名字,姓氏,yearStarted和学分总数。您不应考虑成绩为0的课程,因为它们对应失败的课程”

到目前为止,我有这个:

def q4(self):
    query = '''
    select s.firstName,s.lastName,s.yearStarted,count(*)
    from students s,grades g
    where s.sid = g.sid
    and g.grade >0
    group by s.firstName,s.yearStarted
    '''
    self.cur.execute(query)
    all_rows = self.cur.fetchall()
    return all_rows

并返回以下值:

[('Anne','brown',2020,1), ('Jack','Thomson',2018,3), ('Jacob','McKarthy',2020,2), ('Jamal','Jones',2019,3), ('简','母鹿',2017,2), ('John','Doe',2017,3), ('Tim','Burton',2018,3), ('Tina','Gilligan',2019,3)]

但是显然这些是错误的,当我上传到gradescope时,这些错误位于附件图像中

enter image description here

有什么想法我做错了吗?

解决方法

我已经找到答案了。感谢@ChristianSloper对我的帮助

def q4(self):
    query = '''
    select s.firstName,s.lastName,s.yearStarted,count(*)  as cnt
    from students s,grades g
    where s.sid = g.sid
    and g.grade >0
    group by s.sid,s.yearStarted
    order by s.yearStarted
    desc
     
    '''
    self.cur.execute(query)
    all_rows = self.cur.fetchall()
    return all_rows