SQL - consulta sumar total por rangos

 
Vista:

consulta sumar total por rangos

Publicado por asdfg (21 intervenciones) el 14/05/2009 18:56:59
Hola:

Necesito hacer una consulta en que genere el total por intervalo de edad.
es decir:
edad total
[ 0- 9] 10
[10-15] 15
[16-40] 15
[40- 50] 25

La consulta que realizo es solo para un intervalo no he podido logra que me muestre todos.

select sum (count (t.num_edad)) as uno from registro t where t.fec_ingres>=to_date('01/11/2008','dd/mm/yyyy') and t.fec_ingres<=to_date('30/11/2008','dd/mm/yyyy') and (t.num_edad>=0 and t.num_edad<=9) group by t.num_edad order by t.num_edad;

de antemano gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:consulta sumar total por rangos

Publicado por pacopaz (143 intervenciones) el 14/05/2009 20:14:19
Prueba con esto:

select '[ 0- 9]' as edades, count(1) as uno
from registro t
where t.fec_ingres>=to_date('01/11/2008','dd/mm/yyyy') and t.fec_ingres<=to_date('30/11/2008','dd/mm/yyyy') and (t.num_edad>=0 and t.num_edad<=9)
union all
select '[10-15]' as edades, count(1) as uno
from registro t
where t.fec_ingres>=to_date('01/11/2008','dd/mm/yyyy') and t.fec_ingres<=to_date('30/11/2008','dd/mm/yyyy') and (t.num_edad>=10 and t.num_edad<=15)
union all
select '[16-40]' as edades, count(1) as uno
from registro t
where t.fec_ingres>=to_date('01/11/2008','dd/mm/yyyy') and t.fec_ingres<=to_date('30/11/2008','dd/mm/yyyy') and (t.num_edad>=16 and t.num_edad<=40)
union all
select '[41-50]' as edades, count(1) as uno
from registro t
where t.fec_ingres>=to_date('01/11/2008','dd/mm/yyyy') and t.fec_ingres<=to_date('30/11/2008','dd/mm/yyyy') and (t.num_edad>=41 and t.num_edad<=50)

Espero que te sirva.

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:consulta sumar total por rangos

Publicado por asdfg (21 intervenciones) el 14/05/2009 21:33:17
Funcionó. Te pasaste Gracias.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar