Hello Johan
Again lot of thanks! You have lead me trought the righ path, as by trying the first way you have proposed I have been able to split "consumo" field in two, one of them considerng sales invoices movements and the other the rest of movements (I made a mistake as TransType code for sales invoices is not 14 but 13, but I realized and fix it in query).
Now I have tried to apply same logic to split also "consEUR" in two. By doing it the query looks like this now:
declare @dataInicio datetime
declare @dataFinal datetime
declare @tempo int
SET @tempo=(SELECT TOP 1 T0.TRANSNUM FROM OINM T0 WHERE T0.[DocDate] >=[%0] AND T0.[DocDate] <=[%1] AND T0.TransType != '67')
SET @dataInicio=(SELECT '[%0]')
SET @dataFinal=(SELECT '[%1]')
SELECT
[Artículo]=t1.itemcode,
[Consumo Ventas]=isnull(t1.consumoSO,0),
[Otros consumos]=isnull(t1.consumoREST,0),
[Consumo Ventas EUR]=isnull(t1.consEURSO,0),
[Otros consumos EUR]=isnull(t1.consEURREST,0),
[Días]=isnull(datediff(day,@dataInicio,@dataFinal),0),
[Consumo medio diario]=isnull((t1.consumoSO + t1.consumoREST) /datediff(day,@dataInicio,@dataFinal),0),
[Cons medio diario EUR]=isnull((t1.consEURSO + t1.consEURREST) /datediff(day,@dataInicio,@dataFinal),0),
[Stock inicial]=isnull(coalesce(t0.saldoinic,0),0),
[Stock inicial EUR]=isnull(coalesce(t0.sInicEUR,0),0),
[Stock final]=isnull(coalesce(t0.saldoinic,0)+coalesce(t1.saldofin,0),0),
[Stock final EUR]=isnull(coalesce(t0.sInicEUR,0)+coalesce(t1. sFinEUR,0),0),
[Stock promedio]= isnull((coalesce(t0.saldoinic,0)+(coalesce(t0.saldoinic,0)+t1.saldofin))/2,0),
[Stock promedio EUR]= isnull((coalesce(t0.sInicEUR,0)+(coalesce(t0.sInicEUR,0)+ t1. sFinEUR))/2,0),
[Rotación stock]= case isnull((coalesce(t0.saldoinic,0)+(coalesce(t0.saldoinic,0)+t1.saldofin))/2,0) when 0 then 0 else isnull((t1.consumoSO + t1.consumoREST) / ((coalesce(t0.saldoinic,0)+(coalesce(t0.saldoinic,0)+t1.saldofin))/2) ,0) end,
[Cobertura de stock]= case isnull(coalesce((t1.consumoSO + t1.consumoREST)/datediff(day,@dataInicio,@dataFinal),0),0) when 0 then 0 else isnull(((coalesce(t0.saldoinic,0)+(coalesce(t0.saldoinic,0)+t1.saldofin))/2)/ ((t1.consumoSO + t1.consumoREST)/datediff(day,@dataInicio,@dataFinal)),0) end
FROM (
SELECT
a1.itemcode,
saldoInic=isnull(sum(coalesce(s1.Inqty,0))-sum(coalesce(s1.outqty,0)),0),
sInicEUR=isnull(sum(coalesce(s1.Inqty*a1.AvgPrice,0))-sum(coalesce(s1.outqty*a1.AvgPrice,0)),0)
FROM OITM a1
JOIN OINM s1 on a1.itemcode=s1.itemcode
WHERE s1.docdate<@dataInicio
GROUP BY a1.itemcode
) as t0
RIGHT JOIN (
SELECT
a.itemcode,
entradas=isnull(sum(s.Inqty),0),
entrEUR= isnull((sum(s.Inqty))*a.AvgPrice,0),
consumoSO=(SELECT isnull(sum(outqty),0) FROM OINM WHERE ItemCode = a.ItemCode AND docdate BETWEEN @dataInicio and @dataFinal AND TransType = '13'),
consumoREST=(SELECT isnull(sum(outqty),0) FROM OINM WHERE ItemCode = a.ItemCode AND docdate BETWEEN @dataInicio and @dataFinal AND NOT TransType IN ('67', '13')),
consEURSO=(SELECT isnull((sum(s.outqty))*a.AvgPrice,0) FROM OINM WHERE ItemCode = a.ItemCode AND docdate BETWEEN @dataInicio and @dataFinal AND TransType = '13'),
consEURREST=(SELECT isnull((sum(s.outqty))*a.AvgPrice,0) FROM OINM WHERE ItemCode = a.ItemCode AND docdate BETWEEN @dataInicio and @dataFinal AND NOT TransType IN ('67', '13')),
saldoFin=isnull(sum(s.Inqty)-sum(s.outqty),0),
sFinEUR=isnull((sum(s.Inqty)-sum(s.outqty))*a.AvgPrice,0)
FROM OINM s
JOIN OITM a on a.itemcode=s.itemcode
WHERE s.docdate BETWEEN @dataInicio and @dataFinal AND s.TransType != '67'
GROUP BY a.itemcode, a.AvgPrice ) t1 on t1.itemcode=t0.itemcode
The problem is that by executing it system displays following error message:
1). [Microsoft][SQL Server Native Client 10.0][SQL Server]Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. '' (SWEI)
I have highlighted the parts that have been modified just before getting that error. Without those two parts modified query was running just fine.
Is there something that I have done wrong by adapting your logic to consEUR?
Regards