Решение: включая проверку даты
CREATE TABLE #transaction (
id int NOT NULL,
[date] datetime NULL,
amount float NULL,
balance float NULL,
account_id int NULL
) ;
Insert Into #transaction Values
(1,'2018-11-20',50,4000,100),
(2,'2018-11-21',75,2475,100),
(3,'2018-12-15',75,2400,100),
(4,'2018-11-22',25,4000,200),
(5,'2018-11-22',25,4000,300)
With CTE As
(
Select
ROW_NUMBER() Over(Partition By account_id Order By [Date] Desc) As rn,
account_id, [Date], balance
From #transaction
Where [Date] < '2018-12-01'
)
Select account_id, [Date], balance From CTE
Where rn = 1
Результат:
account_id Date balance
100 2018-11-21 00:00:00.000 2475
200 2018-11-22 00:00:00.000 4000
300 2018-11-22 00:00:00.000 4000