sql server - sql updating column of table whose values come by group by on other table column -
i have 2 table subtopics , userinteractionlog . subtopics have timespent column want update timespent column , value come userinteractionlog using sum(datediff(mi,starttime,endtime)) group subtopicid
this have tried not working...
update subtopics s set timespent = ( select sum(datediff(mi,u.starttime,u.endtime)) userinteractionlog u group u.subtopicid having s.idsubtopic=u.subtopicid )
create table userinteractionlog ( id int , starttime datetime , endtime datetime , subtopicid int ) go insert userinteractionlog(id, starttime, endtime, subtopicid) values(1, '2014-01-01 00:00:00.000', '2014-03-02 00:00:00.000', 1) ,(2, '2014-05-04 00:00:00.000', '2014-06-06 12:00:00.000', 1) ,(3, '2013-01-01 00:00:00.000', '2013-01-02 00:00:00.000', 2) create table subtopics ( timespent bigint , idsubtopic int ) go insert subtopics (timespent, idsubtopic) values (null, 1) ,(null, 2) go update subtopics set timespent = ( select sum(datediff(mi,u.starttime,u.endtime)) userinteractionlog u group u.subtopicid having idsubtopic=u.subtopicid ) go
this returns
timespent idsubtopic 134640 1 1440 2
Comments
Post a Comment