The right answer on the following question:
- That is a bug that was fixed in 11.2.0.3 or later if I recall correctly. (11.2.0.1 is no longer supported anyway. 11.2.0.4 is the only 11.2 release that is still supported) – @a_horse_with_no_name
- The bug number is 11840579 and it was fixed in 11.2.0.3 and 12.1.0.1
– @a_horse_with_no_name
Question
I have a table
CREATE TABLE test(
from_date date,
to_date date
);
INSERT INTO test(from_date,to_date)
--VALUES('20171101','20171115');
VALUES(TO_DATE('20171101','YYYYMMDD'),TO_DATE('20171115','YYYYMMDD'));
The following query in Oracle return only one row (expected 15 rows)
WITH dateCTE(from_date,to_date,d,i) AS(
SELECT from_date,to_date,from_date AS d,1 AS i
FROM test
UNION ALL
SELECT from_date,to_date,d+INTERVAL '1' DAY,i+1
FROM dateCTE
WHERE d<to_date
)
SELECT d,i
FROM dateCTE
SQL Fiddle - http://sqlfiddle.com/#!4/36907/8
For test I changed the condition to i<10
WITH dateCTE(from_date,to_date,d,i) AS(
SELECT from_date,to_date,from_date AS d,1 AS i
FROM test
UNION ALL
SELECT from_date,to_date,d+INTERVAL '1' DAY,i+1
FROM dateCTE
--WHERE d<to_date
WHERE i<10 -- exit condition
)
SELECT d,i
FROM dateCTE
And get the next result
| D | I |
|------------|----|
| 2017-11-01 | 1 |
| 2017-10-31 | 2 |
| 2017-10-30 | 3 |
| 2017-10-29 | 4 |
| 2017-10-28 | 5 |
| 2017-10-27 | 6 |
| 2017-10-26 | 7 |
| 2017-10-25 | 8 |
| 2017-10-24 | 9 |
| 2017-10-23 | 10 |
Why do this recursive query returned bad result in Oracle?
SQL Fiddle - http://sqlfiddle.com/#!4/36907/5
I ran a similar query in SQLServer and I get the right result
WITH dateCTE(from_date,to_date,d,i) AS(
SELECT from_date,to_date,from_date AS d,1 AS i
FROM test
UNION ALL
SELECT from_date,to_date,DATEADD(DAY,1,d),i+1
FROM dateCTE
WHERE d<to_date
)
SELECT d,i
FROM dateCTE
The right result
d i
2017-11-01 1
2017-11-02 2
2017-11-03 3
2017-11-04 4
2017-11-05 5
2017-11-06 6
2017-11-07 7
2017-11-08 8
2017-11-09 9
2017-11-10 10
2017-11-11 11
2017-11-12 12
2017-11-13 13
2017-11-14 14
2017-11-15 15
Why it doesn't work in Oracle? What alternative variants can you suggest? Thank you!
Screen shots from a real system:
See Question&Answers more detail:
os