Talvez isso possa te ajudar
SELECT TD.ItemCode, TD.U_MerchCode,TD.U_CharCode, SUM(TD.Qty), AVG(TD.Unit), AVG(TD.GST)
FROM (
SELECT T2.[ItemCode], T2.[ItemName], T2.[U_MerchCode], T2.[U_CharCode], sum( T1.[Quantity]) as 'Qty', ((sum(T1.[LineTotal])) / (sum( T1.[Quantity])) ) as 'Unit', sum(T1.[LineTotal]) as 'GST' FROM OINV T0 INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry] INNER JOIN OITM T2 ON T1.[ItemCode] = T2.[ItemCode] WHERE T0.[DocCur] = 'AUD' and T1.[Quantity] > 0 and T0.[DocDate] between [%0] and [%1] AND (T2.[U_CharCode] IS NOT NULL or T2.[U_MerchCode] IS NOT NULL) GROUP BY T2.[ItemCode], T2.[ItemName], T2.[U_MerchCode], T2.[U_CharCode]
UNION
SELECT T2.[ItemCode], T2.[ItemName], T2.[U_MerchCode], T2.[U_CharCode], sum( T1.[Quantity])*-1 as 'Qty', ((sum(T1.[LineTotal]))*-1 / (sum( T1.[Quantity]))*-1 ) as 'Unit', sum(T1.[LineTotal])*-1 as 'GST' FROM ORIN T0 INNER JOIN RIN1 T1 ON T0.[DocEntry] = T1.[DocEntry] INNER JOIN OITM T2 ON T1.[ItemCode] = T2.[ItemCode] WHERE T0.[DocCur] = 'AUD' and T1.[Quantity] > 0 and T0.[DocDate] between [%0] and [%1] AND (T2.[U_CharCode] IS NOT NULL or T2.[U_MerchCode] IS NOT NULL) GROUP BY T2.[ItemCode], T2.[ItemName], T2.[U_MerchCode], T2.[U_CharCode]
) GROUP BY TD.ItemCode, TD.U_MerchCode,TD.U_CharCode