Consulta SQL

Para debatir interrogar o exponer cualquier duda o sugerencia sobre el curso SQL desde cero.

Consulta SQL

Notapor ArmaniFar » Jue Mar 02, 2017 7:48 pm

Hola que tal, con permiso del administrador quisiera me pudieran asesorar sobre una consulta.
al realizar dicha consulta por ejemplo:
select clave,count(Clave) as TOTAL from LICENCIAS
where clave='1' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
or clave='2' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
or clave='3' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
group by clave;

tengo el siguiente resultado:
clave TOTAL
------ ------
01 5
02 2
03 5

mi duda es como puedo hacer atra columna que me muestre el porcentaje de cada fila, respecto a TOTAL? muchas gracias.
ArmaniFar
 
Mensajes: 1
Registrado: Jue Mar 02, 2017 7:34 pm

Re: Consulta SQL

Notapor Pere » Sab Mar 25, 2017 11:29 am

Hola ArmaniFar

Lo puedes hacer en dos pasos o integrando una subconsulta, la consulta que te da el total si lo he entendido bien:

select count(Clave) as TOTAL_CLAVES from LICENCIAS
where clave='1' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
or clave='2' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
or clave='3' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'

TOTAL_CLAVES
------------------
12

Hacemos producto cartesiano con la consulta que propones:


select clave,count(Clave) as TOTAL , count(Clave) / TOTAL_CLAVES as * 100 porcentale
from LICENCIAS,
(select count(Clave) as TOTAL_CLAVES from LICENCIAS
where clave='1' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
or clave='2' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
or clave='3' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
)
where clave='1' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
or clave='2' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
or clave='3' and fechaInicio >= '2017' and fechaRetiro is null and Proceso >='2'
group by clave;

espero te sirva
Pere
 
Mensajes: 74
Registrado: Mar Feb 02, 2010 9:44 pm


Volver a Soporte SQL desde cero

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 2 invitados

cron