I want to check continuity of contracts partitioned by CONTRACT_ID
, but I'd like to check contracts only where FLG = 0 and compare it by whole range (with FLG=1
). For example I'll take the first date_to from row where FLG=1
and compare it by every range of dates. If date_to is between some range then I want to qualified the contract. Next date_to and the same situation. I have no idea how bite the problem. A big problem is that I can't use PL/SQL. Can I do it with SQL? Do somebody have idea how to check continuity of contracts in partition by contract_id? I don't know how to order records in partition. I tried few codes and I am giving up...
Thanks for 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 the example: We have three records where FLG=0, so we take a first date_to after some order. I'll use '2016-08-22' and correct procedure should check the date with every range of dates:
'2016-08-22' between '2016-01-01 00:00:00.0' AND '2016-03-11 00:00:00.0'
'2016-08-22' between '2016-04-15 00:00:00.0' AND '2016-06-05 00:00:00.0'
'2016-08-22' between '2016-05-10 00:00:00.0' AND '2016-07-25 00:00:00.0'
'2016-08-22' between '2017-01-19 00:00:00.0' AND '2017-05-21 00:00:00.0'
'2016-08-22' between '2017-03-05 00:00:00.0' AND '2017-04-30 00:00:00.0'
If one of these condition is TRUE then the contract will be 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 will be qualified because '2017-04-30 00:00:00.0'
is between one of ranges:
2017-01-19 00:00:00.0 | 2017-05-21 00:00:00.0
First three rows have 1 beacuse FLG=1