intranet-queries/2016-01-15-Sales_per_payment_per_line.sql

40 lines
1.0 KiB
SQL

-- Sales per payment type per line year to year
-- For Board Meeting presentation
-- Usando tablas forma_pago, formapago_documento, y linea_analisis
SELECT
YEAR(dv.fecha) AS Year,
IF(
fp.dias <= 5,
'cash',
'credit'
) AS Sale_conditions,
l.linea_analisis AS Line,
ROUND(
SUM(dvi.cantidad*i.peso*(IF(dvi.valor>0, 1, -1)))/1000, 2
) AS Weight_MT,
SUM(dvi.valor) AS Value
FROM
documento_ventas dv
JOIN documento_ventas_has_item dvi
USING (id_documento, id_sucursal)
JOIN formapago_documento fpd
USING (id_documento, id_sucursal)
JOIN forma_pago fp
USING (id_formapago)
JOIN item i
USING (id_item)
JOIN view_lineas l
USING (id_linea)
WHERE
(LEFT(dv.id_documento,2) = 'DV' OR
LEFT(dv.id_documento,2) = 'EF' OR
LEFT(dv.id_documento,1) = 'F')
AND NOT (dv.id_cliente LIKE "999")
AND YEAR(dv.fecha) >= 2015
GROUP BY
Year, Sale_conditions, Line