How to get the difference in dates in SQL Server -


i'm having trouble writing query difference between updatedate , creationdate of 2 records if id lowets , difference between recent , second recent updatedate. here's query:

select      a.id, a.requestid, b.krstatus, b.crdate , b.updatedate,      datediff (hour, b.crdate, b.updatedate) timedifference, case when a.id = (select max(a.id) [dbo].[krdocs_hist] a.requestid = 1)     'yes'     else 'no' end ismax, case when a.id = (select min(a.id) [dbo].[krdocs_hist] a.requestid = 1)     'yes'     else 'no' end ismi [dbo].[krdocs_hist] a, [dbo].krdocs_details_hist b      a.requestid = b.requestid     , a.id = b.id      , a.requestid = 1 order b.requestid 

here's current result: enter image description here

what i'd last possible record, check see if there existing 1 before it. if there wasn't compare updatedate , crdate (updatedate minus crdate. if there record before want updatedate minus previous updatedate.

using query:

select b.id, b.requestid, b.updatedate, b.krstatus [dbo].[krdocs_details_hist] b b.requestid = 1 

has result: enter image description here

and using query:

select a.* [dbo].[krdocs_hist] requestid = 1 

has result: enter image description here

update since lag available sql 2012, can use below:

select         id,          requestid,          crdate,          updatedate,          krstatus,          datediff(hour, previousupdatedate, updatedate) timedifference      (select          id,          requestid,          crdate,          updatedate,          krstatus,          lag(updatedate, 1, crdate) on (order year(id)) previousupdatedate      [dbo].krdocs_details_hist) tmp 

i think can try this:

 select      case          when count(*) <= 1  datediff(hour,              (select crdate [dbo].krdocs_details_hist),             (select updatedate [dbo].krdocs_details_hist))         when count(*) > 1 datediff(hour,              (select max(updatedate) [dbo].krdocs_details_hist updatedate < ( select max(updatedate) [dbo].krdocs_details_hist)),             (select max(updatedate) [dbo].krdocs_details_hist))     end timedifference  [dbo].krdocs_details_hist 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -