Thursday, August 13, 2015

Use of "Order by" with "Distinct" keyword in a single query

SELECT DISTINCT name, MAX(id) FROM tbl_student GROUP BY name ORDER BY MAX(id) DESC, name

SELECT DISTINCT name , row_number() OVER (PARTITION BY name ORDER BY id DESC)  AS [rank] FROM tbl_student

No comments: