7
I applied an orientation of another topic and it worked cool when you only have one record in the table with the interval, but I had problems when the same record has more than one date range.
Using the query below:
select trunc(to_date(X.DT_ENTRADA)) + (level-1) periodo
from dual
connect BY level <= to_number(to_date(X.DT_SAIDA) - TO_DATE(X.DT_ENTRADA)) + 1
and having the table "ENTRIES" below as an example:
ID DT_ENTRADA DT_SAIDA CD_PESSOA
1 01/01/2018 05/01/2018 123
2 03/01/2018 07/01/2018 123
3 10/03/2018 15/03/2018 999
4 15/03/2018 17/03/2018 999
How would I set up a single list containing every day between these intervals grouped by person? That is to say:
PERIODO ID CD_PESSOA
01/01/2018 1 123
02/01/2018 1 123
03/01/2018 1 123
03/01/2018 2 123
04/01/2018 1 123
04/01/2018 2 123
05/01/2018 1 123
05/01/2018 2 123
06/01/2018 2 123
07/01/2018 2 123
If you have a way to compile the list containing only the distinct best yet, otherwise I intend to use the good old DISTINCT
on the dates of the list.
I tried to adapt it this way:
SELECT DISTINCT trunc(to_date(X.DT_ENTRADA) + (LEVEL - 1),
X.ID
FROM (
SELECT Y.ID,
Y.DT_ENTRADA,
Y.DT_SAIDA,
Y.CD_PESSOA
FROM ENTRADAS Y
WHERE Y.CD_PESSOA = 123
) X
CONNECT BY LEVEL <= to_number(to_date(X.DT_SAIDA) - TO_DATE(X.DT_ENTRADA)) + 1
but did not roll. The result comes in the same proportion of a Geometric Progression (1,2,4,8,16...), ie in this example will return:
1x 01/01/2018
, 2x 02/01/2018
, 4x 03/01/2018
, 8x 04/01/2018
and 16x 05/01/2018
to the ID 1
and
1x 03/01/2018
, 2x 04/01/2018
, 4x 05/01/2018
, 8x 06/01/2018
and 16x 07/01/2018
to the ID 2
.
I appreciate the help.
and if you have a record like this:
5 02/01/2018 06/01/2018 111
?– Rovann Linhalis
how this record will be unique to the person
111
, then the first query will return the correct list below:02/01/2018
03/01/2018
04/01/2018
05/01/2018
06/01/2018
– MBrenzan
ps. It will always be filtered by just one
CD_Pessoa
?– Rovann Linhalis
Yes, the question I want to answer is: How long did the person stay on site in a given time frame? based on entry and exit dates. This table is simple, but I have to consider several records for the same person in various time intervals that often have no intersection, for example, enters day 01 comes out day 05 then enters again 10 and leaves day 15. We have to take only the days of the interval where the person stayed at the site.
– MBrenzan
I hadn’t understood this part of the intersection of dates, but I think it’s easier to solve
– Rovann Linhalis
one more detail: you only need the intervals that have intersection ?!
– Rovann Linhalis
No I need just the opposite. Following the example of the previous comment, should return me the days
01 02 03 04 05 10 11 12 13 14 15
. If you had entered 01, left 05, then entered again on 05 and left 07 should bring01 02 03 04 05 05 06 07
. Or better still if it was01 02 03 04 05 06 07
without the05
repeated.– MBrenzan
That’s not what’s on the second code I posted?
– Rovann Linhalis
Yes, I’m analyzing the answers and converting here to see if it will happen. Soon put the result
– MBrenzan