If you intend to do this often, it would be worth investing in a table that assigns a unique identifier to each month and start and end dates:
CREATE TABLE MonthEndings ( MonthID INTEGER NOT NULL PRIMARY KEY, StartDate DATE NOT NULL, EndDate DATE NOT NULL ); INSERT INTO MonthEndings VALUES(201101, '27/12/2010', '26/01/2011'); INSERT INTO MonthEndings VALUES(201102, '27/01/2011', '26/02/2011'); INSERT INTO MonthEndings VALUES(201103, '27/02/2011', '26/03/2011'); INSERT INTO MonthEndings VALUES(201112, '27/11/2011', '26/01/2012');
Then you can precisely group using:
SELECT M.MonthID, P.Id, P.Date, P.Amount FROM Payments AS P JOIN MonthEndings AS M ON P.Date BETWEEN M.StartDate and M.EndDate ORDER BY M.MonthID, P.Date;
Any group headers, etc. Itβs best to process it from a DBMS - SQL receives the data in the correct sequence, and the software that receives the data presents it to the user.
If you cannot translate SQL to LINQ, this will make us two. Sorry, I never used LINQ, so I have no idea what that means.
source share