SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to calculate the Leave Entitlement Fiscal Year
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

micnie_2020
Posting Yak Master

Malaysia
226 Posts

Posted - 06/25/2013 :  05:06:22  Show Profile  Reply with Quote
Hi All,



Create table #testLeave
(
User varchar(200),
DateJoin Datetime null,
[Start_Date] Datetime null,
End_Date Datetime null,
GivenDay int null,
rulesincrementyr int null,
increday int null,
maxday int null
)
insert into #testLeave ('Ali','15/Mar/2007','01/Apr/2005','31/Mar/2006','10','1','1','5')

How can i write an stored procedure to exceute the output as above?

Please advise.

Thank you.

Regards,
Micheale

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/25/2013 :  05:19:37  Show Profile  Reply with Quote

;With CTE
AS
(
SELECT Name,DATEADD(yy,1,Start_Date) AS Start_Date,GivenDay AS LeaveEntitlement,IncrementDay,MaxIncrement,IncrementDay AS Counter
FROM #testLeave
UNION ALL
SELECT Name,DATEADD(yy,1,Start_Date)+1,LeaveEntitlement + CASE WHEN Counter + IncrementDay < =  MaxIncrement THEN IncrementDay else 0 END,IncrementDay,MaxIncrement,Counter + IncrementDay
FROM CTE
WHERE DATEADD(yy,1,Start_Date) <= '20150101'
)
SELECT *
FROM CTE
OPTION (MAXRECURSION 0)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000