Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Oracle PLSQL truncar datetimes para blocos de 15 min


Isto irá levá-lo ao trimestre mais próximo.
select sysdate,
       trunc(sysdate,'mi') -                           --truncate to the nearest minute
       numtodsinterval(                                --convert the minutes in number to interval type and subtract.
                       mod(to_char(sysdate,'mi'),15),  --find the minutes from the nearest quarter
                      'minute'                          
                      ) as nearest_quarter
  from dual;

Resultado:
sysdate                             nearest_quarter
-----------------------------------------------------------------
October, 11 2013 05:54:24+0000      October, 11 2013 05:45:00+0000
October, 11 2013 05:22:24+0000      October, 11 2013 05:15:00+0000

Use isso como seu valor inicial e, em seguida, itere sobre isso.
with cte as(
  select trunc(sysdate,'mi') - 
         numtodsinterval(mod(to_char(sysdate,'mi'),15),'minute') as nearest_quarter
  from dual
  )
select nearest_quarter - numtodsinterval((level - 1)*15, 'minute'),
       nearest_quarter - numtodsinterval((level - 2)*15, 'minute')
from cte
connect by level <= 10;

Demonstração .