Last active
September 29, 2023 21:08
-
-
Save leandrogilvalle/5ae57b901428835cfc7230d0ef3d8d82 to your computer and use it in GitHub Desktop.
PL/SQL Oracle - Consulta com With
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with | |
| dts as ( | |
| select | |
| to_date(to_char(max(l.fecha1), 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss') as data_inicial, | |
| to_date(to_char(max(l.fecha2), 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss') as data_final | |
| from debito.log_dia l | |
| where l.fecha1 >= '13/02/2017'--trunc(sysdate) | |
| and l.fecha2 < '14/02/2017'--trunc(sysdate+1) | |
| ), | |
| dif as ( | |
| select dts.data_final - dts.data_inicial as diferenca | |
| from dts | |
| ), | |
| mt as ( | |
| select trunc((((dif.diferenca - trunc(dif.diferenca))*24) - trunc((dif.diferenca - trunc(dif.diferenca))*24))*60) as minutos | |
| from dif | |
| ) | |
| select | |
| Case When mt.minutos < 10 Then 0 | |
| When mt.minutos >= 10 and mt.minutos < 20 Then 1 | |
| When mt.minutos >= 20 Then 2 | |
| End as resultado | |
| from mt | |
| ; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with
dts as (
select l.fecha1 as data_inicial,
l.fecha2 as data_final
-- to_date(to_char(max(l.fecha1), 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss') as data_inicial,
-- to_date(to_char(max(l.fecha2), 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss') as data_final
from debito.log_dia l
where l.fecha1 >= to_date('16/05/2022','dd/MM/yyyy')--trunc(sysdate)
and l.fecha2 < to_date('16/05/2022','dd/MM/yyyy')+1--trunc(sysdate+1)
),
dif as (
select dts.data_final - dts.data_inicial as diferenca
from dts
),
mt as (
select trunc((((dif.diferenca - trunc(dif.diferenca))*24) - trunc((dif.diferenca - trunc(dif.diferenca))*24))60) as minutos
from dif
)
select
/ Case When mt.minutos < 10 Then 0
When mt.minutos >= 10 and mt.minutos < 20 Then 1
When mt.minutos >= 20 Then 2
End as resultado
*/
*
from mt
;