Oracle SQL - how to check continuation of contracts without PL/SQL -


i want check continuity of contracts partitioned contract_id, i'd check contracts flg = 0 , compare whole range (with flg=1). example i'll take first date_to row flg=1 , compare every range of dates. if date_to between range want qualified contract. next date_to , same situation. have no idea how bite problem. big problem can't use pl/sql. can sql? have idea how check continuity of contracts in partition contract_id? don't know how order records in partition. tried few codes , giving up...

thanks every help.

  | contrac_id  |             date_from  |               date_to  | flg      |    |------------ |----------------------- |----------------------- |--------- |   |          1  | 2016-01-01 00:00:00.0  | 2016-03-11 00:00:00.0  |       1  |   |          1  | 2016-04-15 00:00:00.0  | 2016-06-05 00:00:00.0  |       1  |   |          1  | 2016-05-10 00:00:00.0  | 2016-07-25 00:00:00.0  |       1  |   |          1  | 2016-07-25 00:00:00.0  | 2016-08-22 00:00:00.0  |       0  |   |          1  | 2017-01-19 00:00:00.0  | 2017-05-21 00:00:00.0  |       0  |   |          1  | 2017-03-05 00:00:00.0  | 2017-04-30 00:00:00.0  |       0  | 

edit:

on example: have 3 records flg=0, take first date_to after order. i'll use '2016-08-22' , correct procedure should check date every range of dates:

'2016-08-22' between '2016-01-01 00:00:00.0'  ,  '2016-03-11 00:00:00.0' '2016-08-22' between '2016-04-15 00:00:00.0'  ,  '2016-06-05 00:00:00.0' '2016-08-22' between '2016-05-10 00:00:00.0'  ,  '2016-07-25 00:00:00.0' '2016-08-22' between '2017-01-19 00:00:00.0'  ,  '2017-05-21 00:00:00.0' '2016-08-22' between '2017-03-05 00:00:00.0'  ,  '2017-04-30 00:00:00.0'  

if 1 of these condition true contract qualify, else no.

correct select statement results:

  | contrac_id  |             date_from  |               date_to  | flg      | qual|   |------------ |----------------------- |----------------------- |--------- |-----|   |          1  | 2016-01-01 00:00:00.0  | 2016-03-11 00:00:00.0  |       1  |  1  |   |          1  | 2016-04-15 00:00:00.0  | 2016-06-05 00:00:00.0  |       1  |  1  |   |          1  | 2016-05-10 00:00:00.0  | 2016-07-25 00:00:00.0  |       1  |  1  |   |          1  | 2016-07-25 00:00:00.0  | 2016-08-22 00:00:00.0  |       0  |  0  |   |          1  | 2017-01-19 00:00:00.0  | 2017-05-21 00:00:00.0  |       0  |  0  |   |          1  | 2017-03-05 00:00:00.0  | 2017-04-30 00:00:00.0  |       0  |  1  | 

the last contract qualified because '2017-04-30 00:00:00.0' between 1 of ranges:

2017-01-19 00:00:00.0  | 2017-05-21 00:00:00.0 

first 3 rows have 1 beacuse flg=1

if understand correctly looking records flg=1 , can find record date_from column value falls in date range of other record.

don't know how identify individual records assume have id column , table named contracts.

select id contracts c1 c1.flg=1  , exists    (select 1     contracts c2    c1.contrac_id=c2.contrac_id    , c1.id != c2.id    , c1.date_from between c2.date_from , c2.date_to) 

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 -