I have an Activity table with Type,StartDate,EndDate and Amount. I want to calculate the sum of amounts grouped by Type for all the quarters using start date and enddates.
For example, if I have StartDate and EndDate falls between Jan to Mar then sum of all the records' amounts related to that quarter should be calculated.
And I want to do the same for the remaining records.
CREATE TABLE activity (id int(11) ,Type varchar(10) ,StartDate date ,EndDate date ,Amount int(11) ); INSERT INTO activity VALUES (1,'Type1','2021-01-15','2021-02-25',10000), (2,'Type1','2021-01-25','2021-02-25',10000), (3,'Type2','2021-08-05','2021-09-25',15000), (4,'Type3','2021-10-15','2021-12-25',5000); This is the expected output.
EXPECTED OUTPUT:
Type T1 T2 T3 T4 Type1 20000 0 0 0 Type2 0 0 15000 0 Type3 0 0 0 5000 T1,T2,T3,T4 are quarters of Year T1 -> Jan TO Mar T2 -> April TO June T3 -> July TO September T4 -> October TO December I have tried a query. I have given that query in the online editor.
This is the Online editor link with sample data.
Is there anything I'm missing in my query to fetch the correct response?

WITH Activity (...)is a common table expression, I am using this in lieu of going to the trouble of defining and populating a formal table. If your scope already has anActivitytable, then just deleteWITH Activity (...); you don't need it.