PDA

Ver la Versión Completa : cómo sumar


mariajoiz
27-06-2003, 11:39:28
Hola a todos, tengo un problema con una consulta.

Tengo una tabla Facturas con: cod_cliente, cod_factura,fecha, importetotal

Lineas de factura:cod_factura, cod_linea, cod_producto, cantidad, importeparcial

productos: cod_producto, descripción, precioventa, preciocoste

Tengo que hacer un listado con las cantidades vendidas entre 2 fechas (en 1 mes) de cada producto, el totalpreciocoste y el totalprecioventa.

Lo que no tengo claro es cómo sumar las cantidades para cada producto... a ver si me podeis ayudar

gracias

Investment
27-06-2003, 11:51:24
Sacado del Local SQL Help:


Calculates the sum of values for a column.

SUM([ALL] column_reference | DISTINCT column_reference)

Description

Use SUM to sum all the values in the specified column. As an aggregate function, SUM performs its calculation aggregating values in the same column(s) across all rows in a dataset. The dataset may be the entire table, a filtered dataset, or a logical group produced by a GROUP BY clause. Column values of zero are included in the aggregation. NULL column values are not counted in the calculation. If the number of qualifying rows is zero, SUM returns a NULL value.

SELECT SUM(itemstotal)

FROM orders

ALL returns the smallest value for all rows. When DISTINCT is not specified, ALL is the implied default.

DISTINCT ignores duplicate values when calculating the smallest value in the specified column.

MIN returns the smallest value in a column or a calculation using a column performed for each row (a calculated field).

SELECT SUM(itemstotal), SUM(itemstotal * 0.0825) AS TotalTax

FROM orders

When used with a GROUP BY clause, SUM returns one calculation value for each group. This value is the aggregation of the specified column for all rows in each group. The statement below aggregates the total value for the order totals column in the ORDERS table, producing a subtotal for each company in the COMPANY table.

SELECT C."company", SUM(O."itemstotal") AS SubTotal

FROM "customer.db" C, "orders.db" O
WHERE (C."custno" = O."custno")
GROUP BY C."company"

ORDER BY C."company"

Applicability

SUM operates only on numeric values. To use SUM on non-numeric values, first use the CAST function to convert the column to a numeric type.

mariajoiz
27-06-2003, 12:04:55
Hola, gracias investment

yo estaba haciendo algo así, pero no funciona

select sum(L.cantidad)
from facturas F, lineas L, productos P
where F.cod_facturas=L.cod_facturas and P.cod_producto=L.cod_producto and F.fecha
between fecha1 and fecha2
order by P.cod_producto


¿Puedes echarle un vistazo a ver si ves alguna burrada en la consulta?

marto
27-06-2003, 14:00:39
Si lo que quieres es el sumatorio de cada producto te falta agrupar por porducto...



select P.cod_producto, sum(L.cantidad)
from facturas F, lineas L, productos P
where F.cod_facturas=L.cod_facturas
and P.cod_producto=L.cod_producto
and F.fecha between fecha1 and fecha2
group by P.cod_producto