-- Entradas por item por proveedor

SELECT 
	YEAR(e.fecha) AS Ano,
	l.id_linea AS Cod_Linea, 
	l.descripcion AS Linea,
	i.id_item AS Cod_Item, 
	i.descripcion AS Item, 
	p.id_proveedor AS Nit,
	p.nombre_proveedor AS Proveedor,
	SUM(ei.costo*ei.cantidad) AS Costo,
	SUM(ei.cantidad*i.peso) AS Peso

FROM 
	entrada e 
	JOIN entrada_has_item ei 
		ON ( e.id_entrada=ei.id_entrada AND e.id_sucursal=ei.id_sucursal ) 
	JOIN item i 
		ON ( ei.id_item=i.id_item AND ei.id_sucursal=i.id_sucursal ) 
	JOIN linea l
		ON ( l.id_linea = left(i.id_linea,2) AND l.tipo=i.tipo )
        LEFT JOIN proveedor p
		USING (id_proveedor)

WHERE
	YEAR(e.fecha)=2013
GROUP BY 
	Nit, Cod_Item
HAVING
	Nit > 999
ORDER BY
	Nit, Cod_Item

