ms sql中的分组依据和聚合
dbo.edp_GetNumDaysInMonth()
是用户定义的函数。
select
month(a.enddatetime),
cast(((dbo.edp_GetNumDaysInMonth(a.enddatetime)) * 22.5 - (isnull(sum(a.delayhr),0)/3600.00)) / count(a.machine_no) as decimal(11,2)) as MTBF
from mro_maint a
left join mro_machine b on a.machine_no = b.machine_no
where (b.section = 'TRANSMISSION' OR b.section = 'EATON LINE' OR b.section = 'TOYOTA')
group by month(a.enddatetime)
消息 8120,级别 16,状态 1,第 1 行列“a.enddatetime”无效 在选择列表中,因为它不包含在任一聚合中 函数或 GROUP BY 子句。
dbo.edp_GetNumDaysInMonth()
is a user defined function.
select
month(a.enddatetime),
cast(((dbo.edp_GetNumDaysInMonth(a.enddatetime)) * 22.5 - (isnull(sum(a.delayhr),0)/3600.00)) / count(a.machine_no) as decimal(11,2)) as MTBF
from mro_maint a
left join mro_machine b on a.machine_no = b.machine_no
where (b.section = 'TRANSMISSION' OR b.section = 'EATON LINE' OR b.section = 'TOYOTA')
group by month(a.enddatetime)
Msg 8120, Level 16, State 1, Line 1 Column 'a.enddatetime' is invalid
in the select list because it is not contained in either an aggregate
function or the GROUP BY clause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您按
month(a.enddatetime)
进行分组,因此在您的选择中必须使用:month(a.enddatetime)
或您正在使用 < code>a.enddatetime 所以你有一个错误。
在这种情况下,您可能可以使用
min(a.enddatetime)
代替。You are grouping by
month(a.enddatetime)
so in your select you have to use:month(a.enddatetime)
orYou are using
a.enddatetime
so you have an error.In this case probably you can use
min(a.enddatetime)
instead.http://weblogs.sqlteam.com /jeffs/archive/2007/09/10/group-by-month-sql.aspx
http://weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx
每当您将 group by 与任何字段一起使用时,它都应该具有带有 select 选项的聚合函数。
因此,您可以根据选择字段的要求使用 MAX、MIN 等
Whenever you use group by with any field it should have aggregate function with select option.
So you can use MAX, MIN etc. as per requirement with select field