sql - How to find possible consecutive values and return the minimum and the maximum values of that series? -
i'm working on displaying weekly schedule , want merge appointments if consecutive , have no gaps.
for example: have7:00am - 08:00am
, 08:00am
- 03:00pm
time schedule of doctor.
in other words, how find records have consecutive values same doctor , day of week, , return minimum value of start time , maximum value of end time of consecutive set.
is possible retrieve data , convert 07:00am - 03:00pm
using sql? if how?
i'm using sql server 2008 r2
here sample data:
try following query:
;with gatherdates ( select docid, starttime appointmenttime appointment union select docid, endtime appointment ) ,removeduplicates ( select docid, appointmenttime, count(*) duplicatecount gatherdates group docid, appointmenttime ) ,findcontinuosappointment ( select docid, appointmenttime, row_number() over(partition docid order appointmenttime asc) rn removeduplicates duplicatecount = 1 ) select st.docid, st.appointmenttime starttime, et.appointmenttime endtime findcontinuosappointment st inner join findcontinuosappointment et on st.docid = et.docid , st.rn = et.rn - 1 , st.rn % 2 = 1 , et.rn % 2 = 0;
check sql fiddle link: http://www.sqlfiddle.com/#!3/04b71/1/0
do let me know comments.
Comments
Post a Comment