maybe this?
;with miniCalendar
as
(
SELECT
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfThisYear,
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS EndOfThisYear,
dateadd(yy,-1,DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)) StartOfLastYear,
dateadd(yy,-1, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1)) EndOfLastYear,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) startOfLastMonth,
DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) as endOfLastMonth
), lastyear
as
(
select sum(a.Amount) as lastYearSum, Person
From @BATCHDETAIL a
cross join miniCalendar b
where DateOfPayment between b.StartOfLastYear and b.EndOfLastYear + 1
group by Person
), thismonth as
(
select sum(a.Amount) thisMonthSum, Person
From @BATCHDETAIL a
cross join miniCalendar b
where a.DateOfPayment between b.startOfLastMonth and b.endOfLastMonth + 1
group by Person
), thisyear as
(
select sum(a.Amount) thisYearSum, Person
From @BATCHDETAIL a
cross join miniCalendar b
where a.DateOfPayment between b.StartOfThisYear and b.EndOfThisYear + 1
group by Person
)
/*
Ben Smith 10 20 5
Ben Smith paid £10 in 2021, £20 in 2022 to date and £5 in August 2022.
*/
select concat(ly.Person,
' paid £',
ly.lastYearSum,
' in ',
year(StartOfLastYear),
', £',thisYearSum,
' to date and £',
thisMonthSum,
' in ',
DATENAME(mm,startOfLastMonth),
' ',
year(StartOfThisYear)), *
from lastyear ly
cross apply thismonth
cross apply thisYear
cross apply miniCalendar