选择列表中的列 'num_slot.time_slot_id' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。

mxmaxi1234 2012-07-24 11:16:18
select time_slot_id, MAX(student_num) as max_num
from (select COUNT(takes.ID)as student_num, time_slot_id
from takes, section
where takes.course_id=section.course_id
group by time_slot_id) as num_slot(student_num, time_slot_id);

刚学SQL的菜鸟,这地方老报错,不知道为什么,求各位大神解释
...全文
372 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mxmaxi1234 2012-07-25
  • 打赏
  • 举报
回复
我懂了,是我把group by的语法理解错了,谢谢各位大神了~~
百年树人 2012-07-25
  • 打赏
  • 举报
回复
select top 1 COUNT(takes.ID)as student_num, time_slot_id
from takes, section
where takes.course_id=section.course_id
group by time_slot_id
order by student_num desc
mxmaxi1234 2012-07-25
  • 打赏
  • 举报
回复
group by只是在from的子查询里,对外层的查询也有影响吗?
mxmaxi1234 2012-07-25
  • 打赏
  • 举报
回复
这样改的话就不是不是我要的查询结果了,我只要student_num最大的那个元组就行了,如果按再按time_slot_id分组的话,就是把子查询的结果再输出一遍
發糞塗牆 2012-07-25
  • 打赏
  • 举报
回复
如果select里面没有用max、sum、count、min、avg这些的列,都要在group by中出现,另外,其实sql语句的执行顺序不是你写代码的顺序,是先group by 再select的,如果group by中都没有出现这列,select的时候肯定报错。
以学习为目的 2012-07-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
这样改的话就不是不是我要的查询结果了,我只要student_num最大的那个元组就行了,如果按再按time_slot_id分组的话,就是把子查询的结果再输出一遍
[/Quote]

group by 的语法规则就是所有查询结果不是聚集函数的项都要出现在group by 中


建议给出表数据和期望结果。
--小F-- 2012-07-25
  • 打赏
  • 举报
回复
给出数据和所需要的结果。
筱筱澄 2012-07-24
  • 打赏
  • 举报
回复
select time_slot_id, MAX(student_num) as max_num
from (
select COUNT(takes.ID)as student_num, time_slot_id
from takes, section
where takes.course_id=section.course_id
group by time_slot_id
) as num_slot
group by time_slot_id

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧