clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2335904 fiddles created (27465 in the last week).

CREATE TABLE T ( id int, num nvarchar(20), xdate nvarchar(20)); INSERT INTO T VALUES (1,'A1','201703'), (2,'B1','201608'), (3,'A1','202104'), (4,'C1','202006'), (5,'A1','201610'), (6,'B1','201703'); SELECT * FROM T
id num xdate
1 A1 201703
2 B1 201608
3 A1 202104
4 C1 202006
5 A1 201610
6 B1 201703
 hidden batch(es)


SELECT id,num,xdate FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY num ORDER BY xdate DESC) AS NO,* FROM T ) AS K WHERE NO<=2
id num xdate
3 A1 202104
1 A1 201703
6 B1 201703
2 B1 201608
4 C1 202006
 hidden batch(es)