Get Financial Year record from SQL table

Here one of my colleague asked about, how he can get the Financial Year record from his revenue table.
As per India standard Financial Year Starts from Apr-1 and end with Mar-31.
Here is the simple way to calculate and show in grid form to view FY data.

--Create Revenue Table
CREATE TABLE tblRevenue (Id int identity(1,1), Amount numeric(18,2), MonthNumber int, YearNumber int)

--Insert record
INSERT INTO tblRevenue (Amount, MonthNumber, YearNumber)
VALUES
(1500, 1, 2014),
(2500, 2, 2014),
(1300, 3, 2014),
(1600, 4, 2014),
(1100, 5, 2014),
(1300, 6, 2014),
(1500, 7, 2014),
(1600, 8, 2014),
(1700, 9, 2014),
(1800, 10, 2014),
(1900, 11, 2014),
(2200, 12, 2014),
(2300, 1, 2015),
(4500, 2, 2015),
(5600, 3, 2015),
(6000, 4, 2015),
(6400, 5, 2015),
(6100, 4, 2015)

--Get Financial Result (APR to MAR as per Indian FY standard)

DECLARE @curryear as int=2015

SELECT
     Amount
     ,MonthNumber
     ,DATENAME(MONTH,DATEADD(MM, MonthNumber-1,'1900-01-01'))+' '+CAST(YearNumber as varchar) as [MonthYear]
FROM tblRevenue
WHERE  (YearNumber=@curryear-1 AND MonthNumber >= 4)

    OR (YearNumber=@curryear AND MonthNumber <= 3)

Hope it will help you to get FY data on yearly basis from your table.

Comments