Hi,
thank you for both your replies. Just for completeness please find below DDL statements that create the table and populate it with sample data - apologies, I should have included it with my original question.
CREATE TABLE [dbo].[TestTable](
RowID INT IDENTITY (1,1),
[TransactDate] [datetime] NOT NULL
) ON [PRIMARY]
GO
DECLARE @StartDate datetime = {d '2022-01-01'};
DECLARE @EndDate datetime = {d '2022-07-31'};
DECLARE @CurrentDate datetime;
DECLARE @RowCount INT;
DECLARE @NumRows INT;
SET @CurrentDate = @StartDate;
WHILE (@CurrentDate <= @EndDate)
BEGIN
SET @NumRows = CAST(RAND()*1000 AS INT);
SET @RowCount = 1;
WHILE (@RowCount <= @NumRows)
BEGIN
INSERT INTO TestTable (TransactDate) VALUES (@CurrentDate);
SET @RowCount = @RowCount + 1
END
SET @CurrentDate = DATEADD(DAY, 1, @CurrentDate)
END
It seems to me that the answer provided by Viorel-1 does what I need, specifically this query:
with Q as
(
select *,
(row_number() over (order by TransactDate) - 1) / 1000 + 1 as GroupNumber
from MyTable
)
select GroupNumber, min(TransactDate) as Earliest, max(TransactDate) as Latest
from Q
group by GroupNumber