-- Numero de clientes Ene-Sep 2012-2013 por sucursal por linea

SELECT
	dv.id_sucursal AS Suc,
	EXTRACT(YEAR_MONTH FROM dv.fecha) AS Mes,
        COUNT(DISTINCT dv.id_cliente) AS Clientes

FROM
        documento_ventas dv

WHERE
	LEFT(dv.id_documento,2) RLIKE "F|D[^0-9MPBC]" 
	AND (dv.id_cliente != "999")
	AND EXTRACT(YEAR_MONTH FROM dv.fecha) >= 201507
	AND EXTRACT(YEAR_MONTH FROM dv.fecha) <= 201509

GROUP BY
	Mes, Suc

ORDER BY
	Mes, Suc
