sql - Aggregate function exception when I run my query -
this extension of previous question(getting exception datetime diff), time i've made totalbreaktime
float
, below new query.
merge time_tracker target using (select userid, cast(datediff(second,starttime,endtime)/60.0 numeric(36,2)) columnwithbreakscount breakstable convert(date, starttime) = convert(date, getdate()) group userid) source on target.userid = source.userid when matched update set breaks = source.columnwithbreakscount;
this time when run query, i'm getting below exception.
msg 8120, level 16, state 1, line 1 column 'breakstable.starttime' invalid in select list because not contained in either aggregate function or group clause. msg 8120, level 16, state 1, line 1 column 'breakstable.endtime' invalid in select list because not contained in either aggregate function or group clause.
problem:
- as discussed in previous question, want update
time_tracker.breaks
sum ofbreakstable.totalbreaktime
based on date. - when run query(the previous one), considering data instead of current date's data.
and table data below.
time_tracker
breakstable
please let me know going wrong , how can fix this.
thanks
you have subquery in query:
select userid, cast(datediff(second, starttime, endtime)/60.0 numeric(36,2)) columnwithbreakscount breakstable convert(date, starttime) = convert(date, getdate()) group userid;
the second column not aggregated column, nor columns in expression in group by
clause. hence error.
i can speculate want sum()
second column:
merge time_tracker target using (select userid, cast(sum(datediff(second, starttime, endtime)) / 60.0 numeric(36,2)) columnwithbreakscount breakstable convert(date, starttime) = convert(date, getdate()) group userid ) source on target.userid = source.userid when matched update set breaks = source.columnwithbreakscount;
Comments
Post a Comment