关于分组!

drummerzww 2004-08-11 08:54:23
select teacher,class,subject, datediff(minute,starttime,endtime) as hoursum
from tmclassroomclass
group by teacher 出错
去掉 group by teacher 就对了
那位朋友能帮忙 看看sql应该怎么写 谢谢!!!
...全文
95 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-08-11
  • 打赏
  • 举报
回复
--如果只是按teacher分组
select teacher,class=max(class),subject,sum(datediff(minute,starttime,endtime)) as hoursum
from tmclassroomclass
group by teacher
zjcxc 元老 2004-08-11
  • 打赏
  • 举报
回复
举例说明你的要求.
lalakid 2004-08-11
  • 打赏
  • 举报
回复
肯定有重复了
要想没有重复,就把CLASS和SUBJECT去掉就可以了
select teacher sum(datediff(minute,starttime,endtime)) as hoursum
from tmclassroomclass
group by teacher
drummerzww 2004-08-11
  • 打赏
  • 举报
回复
select teacher,sum(datediff(minute,starttime,endtime)) as hoursum
from tmclassroomclass
group by teacher
看来只能 这样写了
非常感谢zjcxc(邹建)
drummerzww 2004-08-11
  • 打赏
  • 举报
回复
谢谢! 不出错 但是用你显示出的teacher有重复 不符合要求
zjcxc 元老 2004-08-11
  • 打赏
  • 举报
回复
上述两种,不知道楼主是那种意思

慨念就是这样的,用group by 的话,在select 中出现的,凡是未包含在聚合函数(sum/max/min等)中的,都必须放在group by中
zjcxc 元老 2004-08-11
  • 打赏
  • 举报
回复
select teacher,class,subject,sum(datediff(minute,starttime,endtime)) as hoursum
from tmclassroomclass
group by teacher,class,subject
zjcxc 元老 2004-08-11
  • 打赏
  • 举报
回复
select teacher,class,subject,datediff(minute,starttime,endtime) as hoursum
from tmclassroomclass
group by teacher,class,subject,datediff(minute,starttime,endtime)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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