I have this data in a table:
service | vat | ini_date | end_date
1 A 10/01/2017 31/01/2017
2 A 15/01/2017 28/02/2017
3 A 15/02/2017 31/03/2017
4 B 15/01/2017 31/03/2017
5 B 15/02/2017 31/03/2017
6 B 20/04/2017 31/05/2017
7 B 20/05/2017 31/05/2017
And I have to group by VAT services which have match in time.
My output should by this:
group service | vat | ini_date | end_date
1 1 A 10/01/2017 31/01/2017
1 2 A 15/01/2017 28/02/2017
1 3 A 15/02/2017 31/03/2017
1 4 B 15/01/2017 31/03/2017
1 5 B 15/02/2017 31/03/2017
2 6 B 20/04/2017 31/05/2017
2 7 B 20/05/2017 31/05/2017
I can't use procedures, I'm trying with some analytical function but there is a lot of casuistics. The problem starts when you have more than three services that coincide in time individually.
Any idea is welcome. Thanks
It looks you want to group services that overlap in time into groups. Since Since Services 1-5 all overlap (like a chain), they become "Group 1". Since the next service (Service 6) doesn't overlap with the previous group, you give it the next id - "Group 2" and continue. Is that correct?
If so...a couple functions that can help you out:
Window functions (to peek at the previous row)
PERIOD data type + related functions to test for overlap