sql server - A tricky query to update the table with values of another table -


this question extension of previous 1 available @ unable know exception in query.

this time i've table named breaks.

and looks below.

enter image description here

i'm able column sum using below query.

select dateadd(second, sum(datediff(second, '19000101', totalbreaktime)), '19000101')  userid = 0138039 , convert(date, starttime) = convert(date, getdate())) t breakstable; 

my second table looks below.

enter image description here

this time, want update breaks column sum of totalbreaktime breaks table(the first screenshot) , condition has date current day.

i'm unable understand how this.

you need merge:

merge secondtable target using (     select  userid,              sum(datediff(second, '19000101', totalbreaktime)) columnwithbreakscount      breakstable     convert(date, starttime) = convert(date, getdate()))     group userid) source on target.userid = source.userid when matched update     set breaks = source.columnwithbreakscount 

but work if have 1 column each userid in secondtable, else need add key kolumn in on part of query, make rows unique.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -