sql >> Database teknologi >  >> RDS >> Oracle

Rekursiv SQL giver ORA-01790

Efter min mening er "Rekursiv Subquery Factoring" brudt i 11g R2 for forespørgsler med dato- eller tidsstempelkolonne.

with test(X) as
(
  select to_date('2010-01-01','YYYY-MM-DD') from dual
  union all (
    select (X + 1) from test where X <= to_date('2010-01-10','YYYY-MM-DD') 
  )
)
select * from test;

ORA-01790
 

brug en cast til at konvertere datatypen:

with test(X) as
(
  select cast(to_date('2010-01-01','YYYY-MM-DD') as date) from dual
  union all (
    select (X + 1) from test where X <= to_date('2010-01-10','YYYY-MM-DD') 
  )
)
select * from test;

X
-------------------
2010-01-01 00:00:00

1 row selected
 

Det hjælper at sætte en date ind i en date, men hvor er de andre resultater?

Det bliver endnu bedre...

Prøv det med en anden startdato:

with test(X) as
(
  select cast(to_date('2007-01-01','YYYY-MM-DD') as DATE) from dual
  union all (
    select (X + 1) from test where X <= to_date('2011-01-11','YYYY-MM-DD') 
  )
)
select * from test 
where rownum < 10; -- important!

X
-------------------
2007-01-01 00:00:00
2006-12-31 00:00:00
2006-12-30 00:00:00
2006-12-29 00:00:00
2006-12-28 00:00:00
2006-12-27 00:00:00
2006-12-26 00:00:00
2006-12-25 00:00:00
2006-12-24 00:00:00

9 rows selected
 

Tælle baglæns? Hvorfor?

Opdatering 14. jan-2014: Som en løsning kan du bruge CTE begyndende med slutdatoen og bygge den rekursive CTE baglæns, sådan her:

with test(X) as
(
  select cast(to_date('2011-01-20','YYYY-MM-DD') as DATE) as x from dual
  union all (
    select cast(X - 1 AS DATE) from test 
    where X > to_date('2011-01-01','YYYY-MM-DD') 
  )
)
select * from test 
 

Resultater:

| X | |--------------------------------| | January, 20 2011 00:00:00+0000 | | January, 19 2011 00:00:00+0000 | | January, 18 2011 00:00:00+0000 | | January, 17 2011 00:00:00+0000 | | January, 16 2011 00:00:00+0000 | | January, 15 2011 00:00:00+0000 | | January, 14 2011 00:00:00+0000 | | January, 13 2011 00:00:00+0000 | | January, 12 2011 00:00:00+0000 | | January, 11 2011 00:00:00+0000 | | January, 10 2011 00:00:00+0000 | | January, 09 2011 00:00:00+0000 | | January, 08 2011 00:00:00+0000 | | January, 07 2011 00:00:00+0000 | | January, 06 2011 00:00:00+0000 | | January, 05 2011 00:00:00+0000 | | January, 04 2011 00:00:00+0000 | | January, 03 2011 00:00:00+0000 | | January, 02 2011 00:00:00+0000 | | January, 01 2011 00:00:00+0000 |

Test udført med:

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
 


  1. mysqli_fetch_array giver mig duplikerede rækker

  2. Hvorfor kan NULL ikke konverteres til JSONs null i postgreSQL?

  3. Sqlalchemy:sekundær forholdsopdatering

  4. Kan ikke indlæse com.mysql.jdbc.Driver