SQL filtrar filas de un sub select
Publicado por Rodrigo Gonzàlez (1 intervención) el 11/12/2015 21:57:08
realicé la siguiente consulta, la cual me entrega la siguiente tabla. el cual quiero quedarme con solo los horarios cuya duración sea la máxima.
para el caso tendría que quedarme entonces:
laboral: Providencia
tiempo libre: Providencia
hogar: Providencia
f01_numero_a_region_municipio______horario_____duracion
84125715____3______Providencia_____hogar______747 <------ MAXIMA DE HOGAR
84125715____7______Curico__________hogar______602
84125715____13_____Huechuraba____ laboral_____335
84125715____13_____Estacion Central_laboral_______8
84125715____13_____Providencia_____laboral_____359 <------- NAXIMA DE LABORAL
84125715____13_____Renca__________laboral______21
84125715____7______Curico__________tiempo libre__38
84125715____14_____Lago Ranco_____tiempo libre__21
84125715____13_____Providencia_____tiempo libre__79 <------- MAXIMA DE TIEMPO LIBRE
de antemano les agradezco cualquier ayuda o noción que tengan para resolver este caso.
saludos.
consulta sql:
para el caso tendría que quedarme entonces:
laboral: Providencia
tiempo libre: Providencia
hogar: Providencia
f01_numero_a_region_municipio______horario_____duracion
84125715____3______Providencia_____hogar______747 <------ MAXIMA DE HOGAR
84125715____7______Curico__________hogar______602
84125715____13_____Huechuraba____ laboral_____335
84125715____13_____Estacion Central_laboral_______8
84125715____13_____Providencia_____laboral_____359 <------- NAXIMA DE LABORAL
84125715____13_____Renca__________laboral______21
84125715____7______Curico__________tiempo libre__38
84125715____14_____Lago Ranco_____tiempo libre__21
84125715____13_____Providencia_____tiempo libre__79 <------- MAXIMA DE TIEMPO LIBRE
de antemano les agradezco cualquier ayuda o noción que tengan para resolver este caso.
saludos.
consulta sql:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
select
f01_numero_a, region, municipio, horario, sum(f01_duracion) as duracion from
(
select f01_numero_a, region, municipio, f01_duracion,
case
when extract('hour' from f01_timestamp) in (9,10,11,12,13,14,15,16,17)
and to_char(f01_timestamp, 'D') between 2 and 6 then 'laboral'
when extract('hour' from f01_timestamp) in (18,19,20,21,22,23,24,0,1,2,3,4,5,6,7,8)
and to_char(f01_timestamp, 'D') between 2 and 6 then 'hogar'
when extract('hour' from f01_timestamp) in (18,19,20,21,22,23,24) and to_char(f01_timestamp, 'D') = 6 then 'tiempo libre'
when to_char(f01_timestamp, 'D') =7 or to_char(f01_fecha, 'D') =1 then 'tiempo libre'
else 'otro'
end as horario
from cdrvoice.refined_201511 as aa, catalogo_celdas_rich as bb
where f01_fecha between '2015-11-10' and '2015-11-20' and f01_numero_a ='84125715'
and aa.f01_antena_latitud_origen1=bb.lat and aa.f01_antena_longitud_origen1=bb.lon
group by 1,2,3,4,5
)
as devtable group by 1,2,3,4
Valora esta pregunta


0