sql - Get active rows between from and to date range inclusive of boundary -
table testtable
has 4 columns
sessionid int
started datetime
ended datetime
sessionisrunning bool
- true if last session has not yet ended. max 1 record can true @ anytime since @ 1 session can running. if no session running records have false.
given 2 dates fromdate
, todate
, how first session started on or after fromdate
, last session ended on or before todate
. tricky condition if session in progress , it's start date >= fromdate
need this. guessing might not possible both min , max session id in 1 sql statement , keep code readable , easy maintain. 2 separate sql statements ok. 1 min , 1 max. can query rows using between min , max.
this last statement explains in different way. sessions started or running , ended or running between from/to dates thank you
after considering edits , comments regarding between, should give result set need. pull record sessionisrunning = true sessions thats started , ended in date range.
select * testtable tt (tt.started >= fromdate , tt.started < dateadd(day, 1, todate) , tt.ended >= fromdate , tt.ended < dateadd(day, 1, todate)) or sessionisrunning = true order tt.sessionid
Comments
Post a Comment