Intena con este Query,
este es el que uso para articulos
DECLARE @FechaInicial DATETIME
DECLARE @FechaFinal DATETIME
SET @FechaInicial=('20140901')
SET @FechaFinal=('20140930')
SELECT
ob.itemcode,
A.ItemName dscription,
SUM(ob.unidades) As unidades
,ROUND(SUM(ob.total),2) as 'Total'
--,(SUM(ob.total)/Sum(ob.unidades)) precio
FROM (
SELECT c.itemcode, SUM(c.unidades) unidades, SUM(c.total) total FROM(
SELECT DISTINCT B.itemcode, SUM(B.unidades) unidades, SUM(total2) total FROM(
SELECT * FROM (
SELECT distinct
T0.DocEntry,
t0.DocNum,
T1.LineNum,
T1.ItemCode as 'itemcode',
T1.InvQty 'unidades',
--T1.Linetotal 'total',
--((t0.DiscPrcnt / 100)) porcentaje,
(T1.Linetotal-(((T0.DiscPrcnt / 100)) * T1.Linetotal)) total2
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
WHERE T0.DocDate between @FechaInicial AND @FechaFinal
AND T0.CANCELED ='N'
GROUP BY
T0.DocEntry,
T0.DocNum,
T1.LineNum,
T1.ItemCode,
T2.NumInCnt,
T1.InvQty,
T1.LineTotal,
T0.DiscPrcnt
)A
)B
GROUP BY B.itemcode
UNION ALL
SELECT DISTINCT B.itemcode, SUM(B.unidades) unidades, SUM(total2) total FROM(
SELECT * FROM (
SELECT distinct
T0.DocEntry,
t0.DocNum,
T1.LineNum,
T1.ItemCode as 'itemcode',
CASE
WHEN T0.CANCELED IN ('N','Y') THEN -T1.InvQty
ELSE T1.InvQty
END 'unidades',
CASE
WHEN T0.CANCELED IN ('N','Y') THEN -(T1.Linetotal-(((t0.DiscPrcnt / 100)) * T1.Linetotal))
ELSE (T1.Linetotal-(((t0.DiscPrcnt / 100)) * T1.Linetotal))
END total2
FROM ORIN T0
INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
WHERE T0.DocDate BETWEEN @FechaInicial AND @FechaFinal
GROUP BY
T0.DocEntry,
T0.DocNum,
T0.CANCELED,
T1.LineNum,
T1.ItemCode,
T2.NumInCnt,
T1.InvQty,
T1.LineTotal,
T0.DiscPrcnt
)A
)B
GROUP BY B.itemcode
)C
GROUP BY c.itemcode
----------------------------------------------------------------------------------------------
) as Ob
INNER JOIN OITM A ON A.ItemCode = ob.itemcode
GROUP BY ob.itemcode, A.numincnt, A.ItemName
ORDER BY itemcode