sql - Find Difference between hours -


i have made query :

select      memberid,         firstname,lastname,         [dateofchange]               ,(select title statuslist idstatus=[oldstatus]) [oldstatus]         ,(select title statuslist idstatus=[newstatus]) [newstatus]     [statuslog] , users     statuslog.iduser=users.iduser 

this query gives me following result:

enter image description here

i have oldstatus , newstatus , dateofchange of status column.

i wanted have difference in hours of status change.

i.e. dateofchange old status oncall patrol want find difference between 2 dates as:

2014-04-04 16:13:33:000 , 2014-04-04 16:13:44:000 

i tried:

select      memberid,         firstname,lastname,         [dateofchange],       datediff(hour,select [dateofchange] statuslog,users idstatus=[oldstatus]       ,select [dateofchange] statuslog,users idstatus=[newstatus])               ,(select title statuslist idstatus=[oldstatus]) [oldstatus]         ,(select title statuslist idstatus=[newstatus]) [newstatus]     [statuslog] , users     statuslog.iduser=users.iduser 

but doesnt worked.

two tables have joined are:

users:

enter image description here

statuslog:

enter image description here

please me.

how can have difference in hours in above query??

edit:

select      memberid,         firstname,lastname,         [dateofchange]     ,        (select      datediff(hour, sl.dateofchange, sln.statusto) statusduration     statuslog sl     outer apply (         select top(1)             dateofchange statusto                     statuslog slt                     sl.iduser = slt.iduser             , slt.dateofchange > sl.dateofchange         order             slt.dateofchange asc     ) sln) hourss        ,(select title statuslist idstatus=[oldstatus]) [oldstatus]         ,(select title statuslist idstatus=[newstatus]) [newstatus]     [statuslog] , users     statuslog.iduser=users.iduser 

edit 2:

enter image description here

here sql query returning desired result using sub select:

select [users].memberid,   [users].firstname,   [users].lastname,   thislog.dateofchange,   statuslist1.title oldstatus,   statuslist2.title newstatus,   (select top 1 datediff(hour,lastlog.dateofchange,thislog.dateofchange)   [dbo].[statuslog] lastlog lastlog.dateofchange<thislog.dateofchange   order dateofchange desc ) hourssincelastchange   [dbo].[statuslog] thislog   inner join [users] on [users].iduser=thislog.iduser   inner join statuslist statuslist1 on statuslist1.idstatus=thislog.oldstatus   inner join statuslist statuslist2 on statuslist2.idstatus=thislog.newstatus   order dateofchange desc 

hopefully got column , table names correct.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -