Minor fix suggestion to original answer
Koen Lostrie, August 12, 2016 - 12:14 pm UTC
Hello Kishore,Connor,
Could it be that there is an error in the argument of MOD_GENERATOR.F_GENERATOR_XREF
To have the date argument incremented by hour the formula should be
trunc(sysdate)+level/24
The code in the question/answer will increment days not hours.
Example:
Original code:
SELECT mylevel,rundate FROM
(
SELECT level as mylevel, TO_CHAR(TRUNC(SYSDATE) + level + 1/24,'DD-MON-YYYY HH24:MI') AS rundate FROM dual CONNECT BY level <= 24
)
WHERE mylevel NOT BETWEEN 4 AND 20;
Original result:
1 13-AUG-2016 01:00
2 14-AUG-2016 01:00
3 15-AUG-2016 01:00
21 02-SEP-2016 01:00
22 03-SEP-2016 01:00
23 04-SEP-2016 01:00
24 05-SEP-2016 01:00
Fixed code:
SELECT mylevel,rundate FROM
(
SELECT level as mylevel, TO_CHAR(TRUNC(SYSDATE) + level/24,'DD-MON-YYYY HH24:MI') AS rundate FROM dual CONNECT BY level <= 24
)
WHERE mylevel NOT BETWEEN 4 AND 20;
Result:
1 12-AUG-2016 01:00
2 12-AUG-2016 02:00
3 12-AUG-2016 03:00
21 12-AUG-2016 21:00
22 12-AUG-2016 22:00
23 12-AUG-2016 23:00
24 13-AUG-2016 00:00
August 14, 2016 - 5:39 am UTC
Yes, nice catch.
Level with connect by works !!
KISHORE Tekali, August 12, 2016 - 1:04 pm UTC
Thank you Sir :
the below sql works for me :
select *
FROM TABLE( MOD_GENERATOR.PKG_GENERATOR_XREF.refcur_to_table_func (
MOD_GENERATOR.F_GENERATOR_XREF (trunc(sysdate)+level+1/24) ))
connect by level <= 24;
FROM DUAL was not needed ...
Level with connect by works !!
KISHORE Tekali, August 12, 2016 - 1:54 pm UTC
Hello Sir,
I observed that the performance by using CONNECT BY LEVEL is quite bad.
works 'fast' for levels upto 2 - anything higher takes a 'long' time - meaning keeps running for 20 minutes or so (and had to kill the session)
whereas a simple UNION ALL of 24 such SQL statements works remarkably fast (except that it clutters the code )
so, I am going with UNION ALL for now until I can find a better performing way to acjieve the same results
Thanks