【问题标题】:Select while ignore duplicate by 2 rows with SQL Server使用 SQL Server 选择同时忽略 2 行重复
【发布时间】:2021-01-10 18:21:15
【问题描述】:

我有这张桌子scan:

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

我想通过 ip+port by last id 选择但忽略重复,所以结果将是

4, 192.168.0.3,2, text4
3, 192.168.0.1,2, text3

如何使用 T-SQL 查询做到这一点?

【问题讨论】:

    标签: sql-server tsql distinct


    【解决方案1】:

    你可以使用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;
    

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 2018-01-11
      • 2021-04-17
      • 2017-08-14
      • 1970-01-01
      • 2016-01-29
      相关资源
      最近更新 更多