【问题标题】:Getting error: Subquery returns more than 1 value出现错误:子查询返回超过 1 个值
【发布时间】:2020-11-01 13:18:32
【问题描述】:

我只是运行这个查询:

select 
    sc.Studentid as [Student ID], StudentName, semester, 
    sum(c.credit) as Creditsemester,
    cast(sum(w.[Weight]*c.credit) / sum(c.credit) as decimal(5, 2)) as [Semester GPA],
    (select sum(c.credit) from StudentScore) as cumulativecredit
from 
    StudentScore sc 
join 
    Student s on s.Studentid = sc.Studentid
join 
    Course c on sc.CourseID = c.CourseID
join 
    [weight] w on sc.Grade = w.GRADE
group by 
    sc.semester, sc.Studentid, s.StudentName

我得到这个错误:

消息 512,第 16 级,状态 1,第 163 行
子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。

有什么办法解决这个问题吗?

【问题讨论】:

  • 样本数据、所需结果和更简单的查询都会有所帮助。
  • 错误在这里告诉你问题;您不了解错误怎么办,我们可以进一步解释。

标签: sql sql-server ssms-2017


【解决方案1】:

这是格式错误:

  (select sum(c.credit) from StudentScore) as cumullativecredit

我不确定您的意图,但您在此处参考了 outer 查询。因为外层查询是聚合查询,sum()指的是外层查询,所以这不是聚合查询。

目前还不清楚你想做什么。如果您在子查询中引用该表,则不会产生 that 错误:

  (select sum(ss.credit) from StudentScore ss) as cumulativecredit

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多