我想要一个sql Server相当于Postgresql distinct on()
a b ---- 1 1 1 2 2 2 2 1 3 3 select distinct on (a) * from my_table a b ---- 1 1 2 2 3 3
我可以在sql Server中做:
select a,min(b) -- or max it does not matter from my_table group by a
解决方法
您可以尝试ROW_NUMBER,但可能会影响您的效果.
;WITH CTE AS ( SELECT *,ROW_NUMBER() OVER(PARTITION BY a ORDER BY b) Corr FROM my_table ) SELECT * FROM CTE WHERE Corr = 1