29 lines
928 B
SQL
29 lines
928 B
SQL
SET NAMES 'utf8';
|
|
|
|
SELECT
|
|
YEAR(dv.fecha) AS year,
|
|
dv.id_sucursal AS sucursal,
|
|
IF(DATEDIFF(dv.fecha_vencimiento, dv.fecha) > 8, "Crédito", "Contado") AS plazo,
|
|
COUNT(DISTINCT(dv.id_cliente)) AS num_clientes,
|
|
COUNT(DISTINCT(dv.id_documento)) AS num_facturas,
|
|
SUM(dvi.valor) AS valor,
|
|
SUM(dvi.cantidad*i.peso*(IF(dvi.valor>0,1,-1))) AS peso
|
|
FROM
|
|
documento_ventas dv
|
|
JOIN documento_ventas_has_item dvi
|
|
ON ( dv.id_documento=dvi.id_documento AND dv.id_sucursal=dvi.id_sucursal )
|
|
JOIN item i
|
|
ON ( dvi.id_item=i.id_item )
|
|
LEFT JOIN linea l
|
|
ON ( l.id_linea = i.id_linea AND l.tipo= i.tipo )
|
|
LEFT JOIN formapago_documento fd
|
|
ON ( fd.id_documento=dv.id_documento AND fd.id_sucursal=dv.id_sucursal )
|
|
LEFT JOIN forma_pago f
|
|
ON ( f.id_formapago=fd.id_formapago )
|
|
|
|
GROUP BY
|
|
year, sucursal, plazo
|
|
|
|
HAVING
|
|
year > 2015 AND year < 2023
|