SQL Server 相当于 PostgreSQL 的 unique on ()
我希望有一个相当于 PostgreSQL distinct on ()
的 Sql Server,
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
但是在有很多列并且查询是临时查询的情况下,它会非常乏味去做。有简单的方法吗?
I would like to have a Sql Server equivalent of the 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
I could do in SQL Server:
select a, min(b) -- or max it does not matter
from my_table
group by a
But in cases where there are many columns and the query is an ad hoc one it is very tedious to do. Is there an easy way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试
ROW_NUMBER
,但这可能会影响您的性能。You can try
ROW_NUMBER
, but it can affect your performance.除了接受的答案之外,您还可以避免使用两个句子并使用像这样的子选择
In addition to the accepted answer, you can avoid using two sentences and use a sub select like this