Select 而忽略与 SQL 服务器的 2 行重复
Select while ignore duplicate by 2 rows with SQL Server
我有这个tablescan
:
id ip port text
-----------------------------
1 192.168.0.1 2 text1
2 192.168.0.1 2 text2
3 192.168.0.1 2 text3
4 192.168.0.3 2 text4
我想 select 但忽略 ip+port 和 last id 的重复,所以结果将是
4, 192.168.0.3,2, text4
3, 192.168.0.1,2, text3
我如何使用 T-SQL 查询来做到这一点?
您可以使用 row_number()
:
select t.*
from (select t.*,
row_number() over (partition by ip, port order by id desc) as seqnum
from t
) t
where seqnum = 1;
我有这个tablescan
:
id ip port text
-----------------------------
1 192.168.0.1 2 text1
2 192.168.0.1 2 text2
3 192.168.0.1 2 text3
4 192.168.0.3 2 text4
我想 select 但忽略 ip+port 和 last id 的重复,所以结果将是
4, 192.168.0.3,2, text4
3, 192.168.0.1,2, text3
我如何使用 T-SQL 查询来做到这一点?
您可以使用 row_number()
:
select t.*
from (select t.*,
row_number() over (partition by ip, port order by id desc) as seqnum
from t
) t
where seqnum = 1;