Friday, February 27, 2015

Spreading Amount across financial year based on Start and End Date

Would have been better if you'd put it in a code block (second button from the right in the editor) but at least you posted it!



DECLARE @table TABLE (PropCode INT ,PropStartDate DATE ,PropEndDate char(10) ,PropRentStartDate DATE ,PropRentEndDate DATE ,MarketRent INT)
INSERT INTO @table(PropCode, PropStartDate, PropEndDate, PropRentStartDate, PropRentEndDate, MarketRent) VALUES
(2718, '2013-01-30','NULL','2012-11-29','2013-07-21',289.20),(2718, '2013-01-30','NULL','2013-07-22','2013-11-24',289.20),
(2718, '2013-01-30','NULL','2013-11-25','2014-06-14',289.20),(2718, '2013-01-30','NULL','2014-06-15','2014-11-30',299.18),
(2718, '2013-01-30','NULL','2014-12-01','2015-01-02',299.18),(2718, '2013-01-30','NULL','2015-01-03','2050-01-01',310.00),
(3901, '2014-05-27','NULL','2014-06-09','2014-11-30',400.00),(3901, '2014-05-27','NULL','2014-12-01','2050-01-01',400.00),
(3960, '2014-10-31','NULL','2014-11-05','2016-11-05',470.00)


DECLARE @rentDate DATETIME = '2014-08-01'

SELECT *
FROM @table
WHERE @rentDate BETWEEN propRentStartDate AND propRentEndDate

SET @rentDate = '2015-03-01'
SELECT *
FROM @table
WHERE @rentDate BETWEEN propRentStartDate AND propRentEndDate



My solution is much different than Eric's, but I said if you posted the ddl and example data I'd take a look, so here it is!




Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question.


No comments:

Post a Comment