Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

micnie_2020
Posting Yak Master

Malaysia
232 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
52326 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  
 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.03 seconds. Powered By: Snitz Forums 2000