Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > SQL
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 27-06-2003
mariajoiz mariajoiz is offline
Miembro
 
Registrado: may 2003
Ubicación: valencia
Posts: 13
Poder: 0
mariajoiz Va por buen camino
cómo sumar

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
Responder Con Cita
  #2  
Antiguo 27-06-2003
Avatar de Investment
Investment Investment is offline
Miembro
 
Registrado: may 2003
Posts: 378
Poder: 22
Investment Va por buen camino
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.
__________________
Saludos,
Peter Investment
Responder Con Cita
  #3  
Antiguo 27-06-2003
mariajoiz mariajoiz is offline
Miembro
 
Registrado: may 2003
Ubicación: valencia
Posts: 13
Poder: 0
mariajoiz Va por buen camino
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?
Responder Con Cita
  #4  
Antiguo 27-06-2003
Avatar de marto
marto marto is offline
Miembro
 
Registrado: may 2003
Ubicación: Barcelona, Catalunya
Posts: 882
Poder: 22
marto Va por buen camino
Si lo que quieres es el sumatorio de cada producto te falta agrupar por porducto...

Código:
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
__________________
E pur si muove
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 22:47:38.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi