SQL Server - Problemas con SUM

 
Vista:

Problemas con SUM

Publicado por jaime (4 intervenciones) el 02/11/2010 07:10:57
Hola estoy voviendo me loco por una consulta la cual no tiene exito
Necesito en listas los campos los gastos y hacer una suma por cada categoria descripcion pero me marcar error

La columna 'Gasto.Cangas' de la lista de selección no es válida, porque no está contenida en una función de agregado ni en la cláusula GROUP BY.

Select Partid.Despar,Gasto.Cangas,sum(Gasto.Cangas) as valor
from Partid,Gasto,Mespre,Presup,Poa,Concep,Capitu
Where Gasto.Par_Id=Partid.Id_Par and
Partid.Con_Id=Concep.Id_Con and
Concep.Cap_Id=Capitu.Id_Cap and
Gasto.Mes_Id=Mespre.Id_Mes and
Mespre.Años_Id=Presup.Id_Año and
Mespre.Id_Poa=Presup.Poa_id and
Presup.Poa_id=Poa.Id_poa and
Mespre.Años_Id=2009 and
Mespre.Id_Poa=10
GROUP by Partid.Despar
order by Partid.Despar;

y asi solo me regresa el total de los gasto pero no los en lista

Select Partid.Despar,sum(Gasto.Cangas) as valor
.. lodemas es igual

1 10101 DIETAS 200
pero suma todos y no los en lista
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:Problemas con SUM

Publicado por Miguel (10 intervenciones) el 02/11/2010 13:42:10
Cualquier campo que añadas al SELECT tendrás que añadirlo en el GROUP BY.

Select Partid.Despar,sum(Gasto.Cangas) as valor
from Partid,Gasto,Mespre,Presup,Poa,Concep,Capitu
Where Gasto.Par_Id=Partid.Id_Par and
Partid.Con_Id=Concep.Id_Con and
Concep.Cap_Id=Capitu.Id_Cap and
Gasto.Mes_Id=Mespre.Id_Mes and
Mespre.Años_Id=Presup.Id_Año and
Mespre.Id_Poa=Presup.Poa_id and
Presup.Poa_id=Poa.Id_poa and
Mespre.Años_Id=2009 and
Mespre.Id_Poa=10
GROUP by Partid.Despar
order by Partid.Despar;

Esto debería devolver los distintos Partid.Despar con la suma de cada uno.
Si sólo te sale un registro mira a ver si el error está en los enlaces que haces entre las distintas tablas.

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:Problemas con SUM

Publicado por jaime  (4 intervenciones) el 02/11/2010 22:47:07
he reducido mi consulta para obetener lo de otra forma
y algo asi como lo mencionaste hice pero me marca error

select Partid.Despar, sum(Gasto.Cangas) as CC from Gasto,Partid
Where Gasto.Par_Id=Partid.Id_Par and
Gasto.Mes_Id between 52 and 63
Group by Partid.Despar

10101 DIETAS 445
10151 SOBRE TABULADO EN CODIGO "H" 12

¿Si quiero agragar otro campo en el select tengo que porner otro group by?

esta seria mi consulta modificada

select Concep.Descon,Partid.Despar, sum(Gasto.Cangas) as total from
Gasto,Partid,Concep
Where Concep.Id_Con=Partid.Con_Id and
Gasto.Par_Id=Partid.Id_Par and
Gasto.Mes_Id between 52 and 63
Group by Partid.Despar
Group by Concep.Descon

Pero me marca error:

Msg 156, Level 15, State 1, Line 6
Sintaxis incorrecta cerca de la palabra clave 'Group'.

quisiera que me regresa lo siguiente:
Conceto X 10101 DIETAS 445
Conceto Y 10151 SOBRE TABULADO EN CODIGO "H" 12
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:Problemas con SUM

Publicado por jaime (4 intervenciones) el 02/11/2010 22:54:50
sorry ya vi que me faltaba pero el resultado fue el siguiente

10100 REMUNERACIONES 10101 DIETAS 445
10100 REMUNERACIONES 10151 SOBRE TABULADO EN CODIGO "H" 12

Otra pregunta si quisiera sumar lo que ya tengo para que se mostrara asi

total X 475
10100 REMUNERACIONES 10101 DIETAS 445
10100 REMUNERACIONES 10151 CODIGO "H" 12

sepuede
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:Problemas con SUM

Publicado por Miguel (10 intervenciones) el 03/11/2010 10:36:14
Prueba esto:

SELECT Concep.Descon, Partid.Despar, SUM(Gasto.Cangas) AS TOTAL
FROM Gasto,Partid,Concep
WHEREConcep.Id_Con=Partid.Con_Id
AND Gasto.Par_Id=Partid.Id_Par
AND Gasto.Mes_Id between 52 and 63
GROUP BY Partid.Despar,Concep.Descon
UNION
SELECT 'Total', 'X', SUM(Gasto.Cangas) AS TOTAL
FROM Gasto,Partid,Concep
WHEREConcep.Id_Con=Partid.Con_Id
AND Gasto.Par_Id=Partid.Id_Par
AND Gasto.Mes_Id between 52 and 63
GROUP BY 'Total', 'X'

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:Problemas con SUM

Publicado por Miguel (10 intervenciones) el 03/11/2010 10:39:47
Perdón, el último Group by no se pone, es así:

SELECT Concep.Descon, Partid.Despar, SUM(Gasto.Cangas) AS TOTAL
FROM Gasto,Partid,Concep
WHEREConcep.Id_Con=Partid.Con_Id
AND Gasto.Par_Id=Partid.Id_Par
AND Gasto.Mes_Id between 52 and 63
GROUP BY Partid.Despar,Concep.Descon
UNION
SELECT 'Total', 'X', SUM(Gasto.Cangas) AS TOTAL
FROM Gasto,Partid,Concep
WHEREConcep.Id_Con=Partid.Con_Id
AND Gasto.Par_Id=Partid.Id_Par
AND Gasto.Mes_Id between 52 and 63

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:Problemas con SUM

Publicado por Miguel (10 intervenciones) el 03/11/2010 10:31:30
Sólo puede haber un group by por sentencia, se hace así:

select Concep.Descon,Partid.Despar, sum(Gasto.Cangas) as total from
Gasto,Partid,Concep
Where Concep.Id_Con=Partid.Con_Id and
Gasto.Par_Id=Partid.Id_Par and
Gasto.Mes_Id between 52 and 63
Group by Partid.Despar,Concep.Descon
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:Problemas con SUM

Publicado por jaime (4 intervenciones) el 06/11/2010 07:29:19
Gracias ! por tu atencion =D lo probare
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:Problemas con SUM

Publicado por Ing Ronny  (1 intervención) el 11/11/2010 19:19:16
Lo que pasa es que cuando usas una funcion de agregado como SUM, AVG, MAX, MIN, etc.. 1ro: si en el query incluyes columnas que no estan en funciones de agregado (EJM: SELECT sum(precio), producto from...) aqui producto no esta en una funcion de agregado, tienes utilizar la clausula GROUP BY y en esta deben estar incluidos todos los campos que estan en el query sin una funcion de agregado

Select Partid.Despar,Gasto.Cangas,sum(Gasto.Cangas) as valor
from Partid,Gasto,Mespre,Presup,Poa,Concep,Capitu
Where Gasto.Par_Id=Partid.Id_Par and
Partid.Con_Id=Concep.Id_Con and
Concep.Cap_Id=Capitu.Id_Cap and
Gasto.Mes_Id=Mespre.Id_Mes and
Mespre.Años_Id=Presup.Id_Año and
Mespre.Id_Poa=Presup.Poa_id and
Presup.Poa_id=Poa.Id_poa and
Mespre.Años_Id=2009 and
Mespre.Id_Poa=10
GROUP by Partid.Despar, Gasto.Cangas
order by Partid.Despar
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