I need to calculate running total premium for each quarter. But my data is broken down by date.
So I need the first row display value based on year and quarter, but if following rows have same Year and Quarter, then value should be 0.
This is what it looks like now:
But I need it like this:
Query with dummy data:
declare @TempTable1 table (ID int, Date date, PolicyNumber varchar(100), Premium money)
insert into @TempTable1
values (1, '2018-01-01', 'Policy1', 100),
(2, '2018-02-08', 'Policy2', 200),
(3, '2018-04-15', 'Policy3', 300),
(4, '2018-05-31', 'Policy4', 150),
(5, '2018-07-10', 'Policy5', 250),
(6, '2018-11-23', 'Policy6', 350),
(7, '2018-12-05', 'Policy7', 330),
(8, '2019-01-09', 'Policy8', 140),
(9, '2019-06-18', 'Policy9', 225),
(10, '2019-06-28', 'Policy10', 145)
SELECT
ID,
Date,
PolicyNumber,
YEAR(date) AS Year,
DATEPART(qq, date) AS Quarter,
Premium,
SUM(premium) OVER (ORDER BY YEAR(date), DATEPART(qq, date)) AS RunningTotal
FROM
@TempTable1
Is it possible to achieve that in same SELECT statement?
Thank you

