Max dates for each sequence within partitions
问题 I would like to see if somebody has an idea how to get the max and min dates within each 'id' using the 'row_num' column as an indicator when the sequence starts/ends in SQL Server 2016. The screenshot below shows the desired output in columns 'min_date' and 'max_date'. Any help would be appreciated. 回答1: Try something like SELECT Q1.id, Q1.cat, MIN(Q1.date) AS min_dat, MAX(Q1.date) AS max_dat FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY id, cat ORDER BY [date]) AS r1, ROW_NUMBER() OVER