MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can SQL combine rows to get contiguous date ranges?

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
ironponygrl
Beginner


Joined: 22 Mar 2013
Posts: 21
Topics: 7
Location: Fort Worth, TX

PostPosted: Wed Apr 09, 2014 11:03 am    Post subject: Can SQL combine rows to get contiguous date ranges? Reply with quote

right now I'm defining a cursor:
Code:

EXEC SQL DECLARE CUR_1 CURSOR FOR                     
  SELECT E.PERSON_ID,                                 
         E.COVERAGE_TYPE,                             
         TO_CHAR(E.START_DATE, 'YYYYMMDD'),           
         TO_CHAR(E.STOP_DATE, 'YYYYMMDD')             
  FROM DHS_ELIG.INT_ELIGIBILITY E, DHS_ELIG.INT_CASE C,
       DHS_ELIG.INT_PERSON P                           
  WHERE E.STATUS = 'Medicaid Eligible'                 
    AND E.PERSON_ID = P.PERSON_ID                     
    AND P.CASE_ID = C.CASE_ID                         
    AND C.CASE_STATUS = 'Completed'                   
    AND E.COVERAGE_TYPE IS NOT NULL                   
    AND E.COVERAGE_TYPE NOT IN ('076','062')           
    AND E.STOP_DATE IS NOT NULL                       
    AND E.STOP_DATE >                                 
        E.START_DATE                                   
  ORDER BY E.PERSON_ID,                               
           E.COVERAGE_TYPE,                           
           E.START_DATE,                               
           E.STOP_DATE                                 
     END-EXEC.

and I'm getting a result set similar to the following:
Code:

FRED, HOME, 20140201, 20140228
FRED, HOME, 20140201, 20150131
FRED, HOME, 20140101, 20140131
FRED, HOME, 20130101, 20130202
MARY, HOME, 20130101, 20150202


but what I really want is to have rows combined such that I show a single row of coverage, when the coverage is logically contiguous. The result set I would like to get is :
Code:

FRED, HOME, 20140101, 20140228
FRED, HOME, 20130101, 20130202
MARY, HOME, 20130101, 20150202

Can I do this with SQL alone?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Wed Apr 09, 2014 1:24 pm    Post subject: Reply with quote

ironponygrl,

What is the format of Start_date and Stop_date i.e how are the columns defined?

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ironponygrl
Beginner


Joined: 22 Mar 2013
Posts: 21
Topics: 7
Location: Fort Worth, TX

PostPosted: Wed Apr 09, 2014 1:52 pm    Post subject: Reply with quote

do you mean what datatype? they are datatype 'DATE'. is that the question?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Wed Apr 09, 2014 2:58 pm    Post subject: Reply with quote

ironponygrl wrote:
do you mean what datatype? they are datatype 'DATE'. is that the question?


ironponygrl,

Yes that is indeed the question. Further clarification, when you say contiguous data records do you mean policies that begin on the first of the month and end on the last day of the month? or when the policy start_date begins after the last policy Stop_date + 1 day ?

Code:

FRED 2013-Jan-01  to 2014-Feb-02
FRED 2014-Jan-01  to 2014-Jan-31
FRED 2014-Feb-01  to 2014-Feb-28
FRED 2014-Feb-01  to 2015-Jan-31

So if I were to pick the contiguous records based on the months then I would pick
Code:

FRED 2014-Jan-01  to 2014-Jan-31
FRED 2014-Feb-01  to 2014-Feb-28


or if I am looking at continuous policy dates then I would pick

Code:

FRED 2013-Jan-01  to 2014-Feb-02
FRED 2014-Feb-01  to 2015-Jan-31


Picking by months is quite easy, but continuous would get tricky

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ironponygrl
Beginner


Joined: 22 Mar 2013
Posts: 21
Topics: 7
Location: Fort Worth, TX

PostPosted: Wed Apr 09, 2014 3:11 pm    Post subject: Reply with quote

Whew! maybe that's why it's not readily apparent to me. I need the 'tricky' answer.

Its not always a full month of coverage. the begin and end dates can be any calendar date, just happens that in my examples I picked 1st and last day of the month.

based on the following input:
Code:

FRED 2013-Jan-01  to 2014-Feb-02
FRED 2014-Jan-01  to 2014-Jan-31
FRED 2014-Feb-01  to 2014-Feb-28
FRED 2014-Feb-01  to 2015-Jan-31

I would like to get a result set that only has one row:
Code:

FRED 2013-JAN-01 to 2015-JAN-31. 

but if there is a break in the coverage, (input below:)
Code:

FRED 2013-Jan-01  to 2013-Jun-02
FRED 2014-Jan-01  to 2014-Jan-31
FRED 2014-Feb-01  to 2014-Feb-28
FRED 2014-Feb-01  to 2015-Jan-31

I would want a single row for each continuous period of coverage:
Code:

FRED 2013-Jan-01  to 2013-Jun-02
FRED 2014-JAN-01  to 2015-Jan-31
Back to top
View user's profile Send private message
ironponygrl
Beginner


Joined: 22 Mar 2013
Posts: 21
Topics: 7
Location: Fort Worth, TX

PostPosted: Wed Apr 09, 2014 5:19 pm    Post subject: Reply with quote

i dont think i answered the question right. if there are two policies and the end date of one and the start date of the other are one day apart, then they are considered 'contiguous', i.e. there never was a lapse in coverage, because coverage is allocated for an entire calendar day. logically, its the same if it was one policy with a longer period between start and end.

what i'm trying to get too, is results from my cursor that represents all of the unbroken periods with a single row for each unbroken period.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Wed Apr 09, 2014 5:59 pm    Post subject: Reply with quote

ironponygrl,

I can't think of an easier way to compare each record and determine if the policy has continuous records. One nasty way is to use recursive SQL and generate all the dates between MIN(Start_date) and Max(Stop_Date) but that would a huge overhead.

Since you are already having the cursor , isn't it easy to handle it in a working storage array? You basically fetch each record and compare it with prior record's stop-date+1 day as the start_date and if it is the same, you simply overlay the prior stop_date with current record stop_date. If that check failed then you add this new record to the array and repeat the process until you are done with key combo. Once done you initialize the array table once again and repeat the process until you are all done with the cursor.

Btw if you just want the Character form of the dates then you can simply use
Code:

REPLACE(CHAR(START_DATE),'-','')


If the date is 2014-03-01 then the output is 20140301.

Doesn't the TO_CHAR function return a variable string?

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ironponygrl
Beginner


Joined: 22 Mar 2013
Posts: 21
Topics: 7
Location: Fort Worth, TX

PostPosted: Thu Apr 10, 2014 8:30 am    Post subject: Reply with quote

Thanks Kolusu. I've long been aware of the manipulation in a working storage array, but the problem has been around so long, (everybody in the insurance or public assistance arena has to wrestle with this problem) I was just wondering if anybody had come up with a simple SQL solution yet.

seems like years ago I did the recursive SQL thing too, I remember sucky performance. I was curious if some new function had been developed that I was unaware of. wishful thinking ...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group