Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

Sumar grupos y luego generar total en mysql

Estas en el tema de Sumar grupos y luego generar total en mysql en el foro de Mysql en Foros del Web. NECESITO HACER UNA CONSULTA QUE AGRUPE SUMANDO LAS CATEGORIAS Y LUEGO GENERE UN TOTAL EN LA ULTIMA FILA, ME EXPLICO CON UN EJEMPLO: TABLA Categoria ...
  #1 (permalink)  
Antiguo 22/12/2010, 08:10
 
Fecha de Ingreso: diciembre-2010
Mensajes: 8
Antigüedad: 13 años, 3 meses
Puntos: 0
Exclamación Sumar grupos y luego generar total en mysql

NECESITO HACER UNA CONSULTA QUE AGRUPE SUMANDO LAS CATEGORIAS
Y LUEGO GENERE UN TOTAL EN LA ULTIMA FILA, ME EXPLICO CON UN EJEMPLO:

TABLA

Categoria Valor
-----------------------
ELECTRICISTA 20
MECANICO 30
MECANICO 30
MECANICO 30
DIBUJANTE 40
DIBUJANTE 40
----------------------

RESULTADO QUE NECESITO:

---------------------------
Categoria Suma
---------------------------
ELECTRICISTA 20
MECANICO 90
DIBUJANTE 80
TOTAL 190
------------------------

EN PHP GENERAR EL TOTAL SERIA FACILISIMO,
PERO GENERAR UNA FILA CON EL TOTAL EN MYSQL NO SE SI SE PODRA
AGRADECERIA CUALQUIER AYUDA.
  #2 (permalink)  
Antiguo 22/12/2010, 08:18
Avatar de Snaft_J1  
Fecha de Ingreso: diciembre-2006
Mensajes: 285
Antigüedad: 17 años, 3 meses
Puntos: 8
Respuesta: Sumar grupos y luego generar total en mysql

Código MySQL:
Ver original
  1. Select Categoria, SUM(Valor)
  2. From TU_TABLA
  3. Group By Categoria

ya luego genera el total desde php o haces otra consulta con el sum general sin agrupaciones.


saludos
__________________
-= El conocimiento y el saber te hacen un ser libre =-
Ando en busca de conocimiento....
  #3 (permalink)  
Antiguo 22/12/2010, 09:24
Avatar de gnzsoloyo
Moderador criollo
 
Fecha de Ingreso: noviembre-2007
Ubicación: Actualmente en Buenos Aires (el enemigo ancestral)
Mensajes: 23.324
Antigüedad: 16 años, 4 meses
Puntos: 2658
Respuesta: Sumar grupos y luego generar total en mysql

Código MySQL:
Ver original
  1. SELECT Categoria, SUM(Valor)
  2. FROM TU_TABLA

Eso entrega una línea más con el total de todos.
__________________
¿A quién le enseñan sus aciertos?, si yo aprendo de mis errores constantemente...
"El problema es la interfase silla-teclado." (Gillermo Luque)
  #4 (permalink)  
Antiguo 23/12/2010, 09:25
 
Fecha de Ingreso: diciembre-2010
Mensajes: 8
Antigüedad: 13 años, 3 meses
Puntos: 0
De acuerdo Respuesta: Sumar grupos y luego generar total en mysql

MUCHAS GRACIAS gnzsoloyo POR TU RESPUESTA ME HA SIDO DE MUCHA UTILIDAD.
TAMBIEN ENCONTRE UNA BUENA SOLUCION YO MISMO Y QUIERO COMPARTIRLA CON USTEDES:

SE TRATA DE AGREGAR UNA CATEGORIA llamada "*" (QUE VENDRIA SIENDO PARA TODAS LAS CATEGORIAS) Y USAR EL CONDICIONAL "IF"

Código:
Select 
Categoria,
if (Categoria in ('ELECTRICISTA','MECANICO','DIBUJANTE'),sum(Valor),(select sum(T1.Valor) from MITABLA T1)) suma
from MITABLA group by Categoria
having Categoria in ('ELECTRICISTA','MECANICO','DIBUJANTE','*')
order by Categoria desc
TAMBIEN SE PUEDE HACER CON EL "CASE" EN LUGAR DE "IF"
ESPERO LES SEA DE UTILIDAD

GRACIAS A TODOS
FELIZ NAVIDAD!!!!!!!!!

Etiquetas: suma, sumar, totales
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:14.