
CREATE VIEW [dbo].[ApiHolidaysLimitView]
as
select top 100 percent
ll.*, YearLimit+FromPreviousYear Total,
YearLimit+FromPreviousYear - Used Remaining
from (select Id, Pracownik WorkerId,
YEAR(OkresFrom) Year,
cast(LimitGodz/60  as decimal) YearLimit , 
cast ( WykorzystaneGodz/60 as decimal) Used,
--(LimitGodz- WykorzystaneGodz)/60 Remaining , 
cast(ISNULL((select SUM(LimitGodz)- SUM(WykorzystaneGodz)  from  dbo.LimNieobecnosci wg  
where wg.Definicja = l.Definicja and wg.Pracownik  = l.Pracownik and wg.OkresTo< l.OkresFrom),0)/60 as decimal) FromPreviousYear
 from dbo.LimNieobecnosci l where Definicja=1
 ) ll
--and Id = 16955
ORDER BY YEAR DESC






