-- Ventas por mes, item

SELECT 
	DATE_FORMAT(dv.fecha,"%Y-%m") AS Mes,
	i.id_item AS Codigo, 
	i.descripcion AS Item, 
	SUM(dvi.valor) AS Valor,
	SUM(dvi.cantidad) AS Cantidad,
	i.unidad1 AS UoM 

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 
			AND dvi.id_sucursal=i.id_sucursal ) 

WHERE
	LEFT(dv.id_documento,2) RLIKE "F|D[^0-9MPBC]" 
	AND NOT (i.tipo LIKE "5") 
	AND NOT (dv.id_cliente LIKE "999") 
	AND YEAR(dv.fecha)=@ANO

GROUP BY 
	Item,Mes

ORDER BY
	Item,Mes

