我有一个非常基本的问题:
我有这个代码,它可以工作并返回23 行:
select
Issue_ID
,sum(HoursSpent) as TimeSpent
--,DateClosed
from SixMonthsReview
where StaffAssigned = 'Teddy Bear'
and DateClosed between '2018-07-01 06:36:26.790' and '2018-08-01 06:36:26.790'
group by Issue_ID
但是如果我取消注释第 4 行,我会收到一个错误:
Msg 8120, Level 16, State 1, Line 4
Column 'SixMonthsReview.DateClosed' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
所以我添加DateClosed
到GROUP BY
但现在返回了40 行,我可以看到双打。
我哪里错了?
group by Issue_ID
表示每行Issue_ID
,因此您需要告诉它如何显示DateClosed
为单个值。也许你想要MAX(DateClosed)
例如?