Estou com dificuldade em qual a melhor forma de fazer essas consultas
- Para cada mês do ano anterior ao ano corrente, retornar a quantidade de carros estacionados por período (de 00:00 a 07:59, 08:00 a 15:59 e 16:00 a 23:59)
- Fazer uma consulta semelhante à anterior, porém em termos de porcentagem
Consultar período de horas
- dr_gori
- Moderador
- Mensagens: 5024
- Registrado em: Seg, 03 Mai 2004 3:08 pm
- Localização: Portland, OR USA
- Contato:
Thomas F. G
Você já respondeu a dúvida de alguém hoje?
https://glufke.net/oracle/search.php?search_id=unanswered
Você já respondeu a dúvida de alguém hoje?
https://glufke.net/oracle/search.php?search_id=unanswered
Eu fiz assim:
Veja:
Pegando o resultado disso, basta contar quantos tem em cada grupo:
Isso vai gerar assim:
Ai o percentual, basta fazer as contas acima.
Eu vou usar função analítica SUM pra pegar o total em tempo real.
Resultado:
select
to_char(creation_date, 'dd/mm/yyyy hh24:mi') diahora
, case when to_char(creation_date, 'hh24:mi')>='00:00'
and to_char(creation_date, 'hh24:mi')<='07:59' then '0 as 8'
when to_char(creation_date, 'hh24:mi')>='08:00'
and to_char(creation_date, 'hh24:mi')<='15:59' then '8 as 16'
when to_char(creation_date, 'hh24:mi')>='16:00'
and to_char(creation_date, 'hh24:mi')<='23:59' then '16 as 0'
end grupo
from tabela
DIAHORA GRUPO
---------------- -------
21/07/2018 18:22 16 as 0
28/03/2020 04:54 0 as 8
28/03/2020 11:55 8 as 16
28/03/2020 11:57 8 as 16
23/03/2020 14:39 8 as 16
28/03/2020 05:09 0 as 8
28/03/2020 05:14 0 as 8
15/05/2014 06:12 0 as 8
22/02/2015 13:35 8 as 16
19/06/2014 03:57 0 as 8
27/03/2020 07:08 0 as 8
31/10/2019 11:01 8 as 16
31/10/2019 06:26 0 as 8
31/10/2019 07:50 0 as 8
31/10/2019 10:56 8 as 16
08/05/2014 11:12 8 as 16
09/05/2014 07:03 0 as 8
19/06/2014 04:03 0 as 8
20/02/2015 14:52 8 as 16
09/07/2014 08:39 8 as 16
select
grupo
, count(*)
from (
select
to_char(creation_date, 'dd/mm/yyyy hh24:mi') diahora
, case when to_char(creation_date, 'hh24:mi')>='00:00'
and to_char(creation_date, 'hh24:mi')<='07:59' then '0 as 8'
when to_char(creation_date, 'hh24:mi')>='08:00'
and to_char(creation_date, 'hh24:mi')<='15:59' then '8 as 16'
when to_char(creation_date, 'hh24:mi')>='16:00'
and to_char(creation_date, 'hh24:mi')<='23:59' then '16 as 0'
end grupo
from tabela
)
group by grupo
GRUPO COUNT(*)
------- ----------
16 as 0 1584
0 as 8 2519
8 as 16 4295
SQL>
Eu vou usar função analítica SUM pra pegar o total em tempo real.
select
grupo
, qtd
, trunc(qtd / sum(qtd) over()* 100, 4 ) PERC
from (
select
grupo
, count(*) qtd
from (
select
to_char(creation_date, 'dd/mm/yyyy hh24:mi') diahora
, case when to_char(creation_date, 'hh24:mi')>='00:00'
and to_char(creation_date, 'hh24:mi')<='07:59' then '0 as 8'
when to_char(creation_date, 'hh24:mi')>='08:00'
and to_char(creation_date, 'hh24:mi')<='15:59' then '8 as 16'
when to_char(creation_date, 'hh24:mi')>='16:00'
and to_char(creation_date, 'hh24:mi')<='23:59' then '16 as 0'
end grupo
from tabela
)
group by grupo
)
GRUPO QTD PERC
------- ---------- ----------
16 as 0 1584 18.8616
0 as 8 2519 29.9952
8 as 16 4295 51.1431
-
- Informação
-
Quem está online
Usuários navegando neste fórum: Nenhum usuário registrado e 41 visitantes