Olá Kenneth
Eu fiz isso, pode te servir de guia
--SET Arithabort OFF
--SET ANSI_WARNINGS OFF
/* SELECT FROM [dbo].[OINM] T1 */
DECLARE @dateInicio DATETIME
/* WHERE */
SET @dateInicio = '20150101'--/* T1.DocDate */ '[%0]'
/* SELECT FROM [dbo].[OINM] T1 */
DECLARE @dateFinal DATETIME
/* WHERE */
SET @dateFinal = '20150301'--/* T1.DocDate */ '[%1]'
/* SELECT FROM [dbo].[OITB] T2 */
DECLARE @grupodesde AS NVARCHAR(30)
/* WHERE */
SET @grupodesde = ''--/* T1.ItmsGrpNam */ '[%2]'
/* SELECT FROM [dbo].[OITB] T2 */
DECLARE @grupohasta AS NVARCHAR(30)
/* WHERE */
SET @grupohasta = ''--/* T1.ItmsGrpNam */ '[%3]'
/* SELECT FROM [dbo].[OITM] T0 */
DECLARE @productodesde AS NVARCHAR(30)
/* WHERE */
SET @productodesde = ''--/* T0.ItemCode */ '[%4]'
/* SELECT FROM [dbo].[OITM] T0 */
DECLARE @productohasta AS NVARCHAR(30)
/* WHERE */ SET @productohasta = ''--/* T0.ItemCode */ '[%5]'
select t0.articulo [Artigo], t0.ItemName [Descrição], t3.Consumo*t1.Price 'Consumo',
((t3.saldoinic-t3.salidas)* t1.Price) 'Stock Inicial $',
cast(ISNULL(X2.Compras,0)as decimal(16,2))'Compras',
cast(ISNULL(X3.Vendas,0) as decimal(16,2)) 'Vendas',
(t1.SaldoFin * t1.Price) 'Stock Final $',
cast(ISNULL(X3.CustoVenda,0) as decimal (16,2)) 'Custo Venda' ,
cast(ISNULL((X3.Vendas-X3.CustoVenda),0)as decimal (16,2))'Ganancia',
convert(decimal (16,2),case isNull(X3.Vendas,0)
when 0 then 0 else ((IsNull(X3.Vendas,0)-IsNull(X3.CustoVenda,0))/IsNull(X3.Vendas,0) *100) end)'%',
round(convert(decimal (16,2),case ((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) +IsNull(t1.SaldoFin,0)) /2)
when 0 then 0 else (IsNull(t3.Consumo,0)) / nullif(((IsNull(t3.saldoInic,0) +
IsNull(t3.saldoInic,0) +IsNull(t1.SaldoFin,0)) /2),0) end),-1)'Rotação Vezes',
--(365)/
case when
(
round(convert(decimal (16,2),case ((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) +IsNull(t1.SaldoFin,0)) /2)
when 0 then 0 else (IsNull(t3.Consumo,0)) /
((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) + IsNull(t1.SaldoFin,0)) /2) end),-1)) = 0 then 0
else
(365)/
(round(convert(decimal (16,2),case ((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) +IsNull(t1.SaldoFin,0)) /2)
when 0 then 0 else (IsNull(t3.Consumo,0))/
((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) + IsNull(t1.SaldoFin,0)) /2) end),-1)) end
'Rotação Dias',
(CASE WHEN EntMas = 0 THEN (EntMas*t1.Price)
WHEN EntMas > 0 AND EntMas < = SaltoT THEN 0
WHEN SaltoT > 0 AND EntMas > = SaltoT THEN (EntMas - SaltoT)*T1.Price
WHEN SaltoT = 0 AND EntMas &