Que tal buen día comunidad, espero no molestar y si alguien puede ayudarme lo agradecería, tengo muy poco en el uso del aplicativo y algunos temas de repente se me complican, la contadora de la empresa me comento si se puede adaptar un query que ya esta creado, necesita seleccionar el año y el actual hace la consulta al año en curso, anexo el codigo.
declare @ultima_amor date
declare @mes numeric (19,6)
declare @periodo numeric (19,6)
declare @cursor numeric (19,6)
declare @año numeric (19,6)
set @ultima_amor = (select max(t0.keydate) from odrn t0)
set @mes = month(@ultima_amor)
set @periodo = (select max (t0.periodcat) from itm8 t0)
set @cursor = 1
set @año = year(@ultima_amor)
SELECT Distinct T0.ItemCode as [Codigo del Activo],
T0.ItemName as [Nombre del activo],
T0.AssetClass as [Clase de activo],
T1.DprStart as [Fecha Inicio de Depreciacion],
T1.DprEnd as [Fecha Fin de Depreciacion],
T1.UsefulLife as [Vida util],
case when @año = year(T1.DprStart) then case when @mes = month(T1.DprStart) then (t1.RemainLife -1) else T1.RemainLife - (@mes - (month(t1.DprStart) - 1)) end else (T1.RemainLife - @mes) end as [Resto de vida],
case when @año = year(T1.DprStart) then T3.LineTotal else T2.APC end as [Capitalizacion],
case when @año = year(T1.DprStart) then T3.LineTotal / T1.usefulLife else (T2.APC / T1.UsefulLife) end as [depreciacion por mes],
case when @año = year(T1.DprStart) then case when month(T1.DprStart) <= @mes then (T3.LineTotal / T1.UsefulLife) * (@mes - (month(t1.DprStart) - 1)) else 0 end else (((T2.APC / T1.UsefulLife) * @mes) + T2.OrDpAcc) end as [Depreciacion acumulada],
case when @cursor <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 1 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 1 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Enero],
case when @cursor + 1 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 2 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 2 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Febrero],
case when @cursor + 2 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 3 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 3 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Marzo],
case when @cursor + 3 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 4 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 4 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Abril],
case when @cursor + 4 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 5 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 5 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Mayo],
case when @cursor + 5 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 6 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 6 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Junio],
case when @cursor + 6 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 7 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 7 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Julio],
case when @cursor + 7 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 8 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 8 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Agosto],
case when @cursor + 8 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 9 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 9 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Septiembre],
case when @cursor + 9 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 10 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 10 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Octubre],
case when @cursor + 10 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 11 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 11 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Noviembre],
case when @cursor + 11 <= @mes then case when @año = year(T1.DprStart) then case when month(T1.DprStart) = 12 then (T3.LineTotal/T1.UsefulLife) else case when month(T1.DprStart) > 12 then 0 else T3.LineTotal/T1.UsefulLife end end else T2.APC / T1.UsefulLife end end as [Diciembre]
FROM OITM T0 INNER JOIN ITM7 T1 ON T0.ItemCode = T1.ItemCode INNER JOIN ITM8 T2 ON T1.ItemCode = T2.ItemCode LEFT JOIN ACQ1 T3 ON T0.ItemCode = T3.ItemCode
WHERE T1.RemainLife <> 0 and @ultima_amor < t1.DprEnd and T1.PeriodCat = @periodo and T2.PeriodCat = @periodo
Agradezco al que pueda ayudarme