SQL Server: SUM() de varias filas incluyendo las cláusulas where
Tengo una tabla que se parece a la siguiente:
PropertyID Amount Type EndDate
--------------------------------------------
1 100 RENT null
1 50 WATER null
1 60 ELEC null
1 10 OTHER null
2 70 RENT null
2 10 WATER null
Habrá varios artículos facturados a una propiedad, también facturados varias veces. Por ejemplo, EL ALQUILER podría facturarse a la propiedad #1 12 veces (más de un año), sin embargo, los únicos que me interesan son aquellos con ENDDATE de null (en otras palabras, actual)
Me gustaría lograr:
PropertyId Amount
--------------------------
1 220
2 80
He tratado de hacer algo como esto :
SELECT
propertyId,
SUM() as TOTAL_COSTS
FROM
MyTable
Sin embargo, en la SUMA me vería obligado a tener múltiples selecciona traer de vuelta la cantidad actual para cada tipo de carga? Pude ver que esto se vuelve desordenado y espero una solución mucho más simple
¿Alguna idea?
6 answers
Esto devolverá los totales por propiedad y tipo
SELECT PropertyID,
TYPE,
SUM(Amount)
FROM yourTable
GROUP BY PropertyID,
TYPE
Esto devolverá solo los valores activos
SELECT PropertyID,
TYPE,
SUM(Amount)
FROM yourTable
WHERE EndDate IS NULL
GROUP BY PropertyID,
TYPE
Y esto devolverá los totales de las propiedades
SELECT PropertyID,
SUM(Amount)
FROM yourTable
WHERE EndDate IS NULL
GROUP BY PropertyID
......
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-07 06:56:44
Prueba esto:
SELECT
PropertyId,
SUM(Amount) as TOTAL_COSTS
FROM
MyTable
WHERE
EndDate IS NULL
GROUP BY
PropertyId
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-22 14:50:58
Te refieres a getiing sum (Cantidad de todos los tipos) para cada propiedad donde endDate es null:
SELECT propertyId, SUM(Amount) as TOTAL_COSTS
FROM MyTable
WHERE EndDate IS NULL
GROUP BY propertyId
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-22 14:45:17
Parece que quieres algo como:
select PropertyID, SUM(Amount)
from MyTable
Where EndDate is null
Group by PropertyID
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-22 14:47:06
La cláusula WHERE
siempre se aplica conceptualmente (el plan de ejecución puede hacer lo que quiera, obviamente) antes de la GROUP BY
. Debe venir antes de GROUP BY
en la consulta, y actúa como un filtro antes de las cosas son SUM
med, que es como la mayoría de las respuestas aquí funcionan.
También debe tener en cuenta la cláusula opcional HAVING
que debe venir después de el GROUP BY
. Esto se puede usar para filtrar las propiedades resultantes de los grupos después de GROUP
ing-por ejemplo HAVING SUM(Amount) > 0
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-22 14:56:45
Use una expresión de tabla común para agregar la fila del total general, top 100
es necesario para que order by
funcione.
With Detail as
(
SELECT top 100 propertyId, SUM(Amount) as TOTAL_COSTS
FROM MyTable
WHERE EndDate IS NULL
GROUP BY propertyId
ORDER BY TOTAL_COSTS desc
)
Select * from Detail
Union all
Select ' Total ', sum(TOTAL_COSTS) from Detail
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-29 09:50:05