T-SQL Tuesday #168 - Top N Per Group example 2

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

WITH UsersThisYear AS
(
  SELECT Id, DisplayName, CreationDate,
    rn = ROW_NUMBER() OVER (
    PARTITION BY DATEFROMPARTS(YEAR(CreationDate), MONTH(CreationDate), 1)
    ORDER BY CreationDate)
  FROM Users
  WHERE CreationDate >= DATEFROMPARTS(YEAR(GETDATE()),1,1)
)
SELECT Id, DisplayName, CreationDate
FROM UsersThisYear
WHERE rn = 1
ORDER BY CreationDate;

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results