sql - 仅根据表的一列消除重复值

标签 sql sql-server distinct inner-join duplicate-removal

我的查询:

SELECT sites.siteName, sites.siteIP, history.date
FROM sites INNER JOIN
     history ON sites.siteName = history.siteName
ORDER BY siteName,date

输出的第一部分:

enter image description here

如何删除 siteName 中的重复项柱子?我只想留下基于 date 的更新的柱子。

在上面的示例输出中,我需要第 1、3、6、10 行

最佳答案

这就是窗口函数row_number()的地方派上用场:

SELECT s.siteName, s.siteIP, h.date
FROM sites s INNER JOIN
     (select h.*, row_number() over (partition by siteName order by date desc) as seqnum
      from history h
     ) h
    ON s.siteName = h.siteName and seqnum = 1
ORDER BY s.siteName, h.date

关于sql - 仅根据表的一列消除重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17507887/

相关文章:

sql - 检查子查询中是否存在记录

MySQL 选择不同的问题

SQL查询: Using DISTINCT/UNIQUE and SUM() in one statement

Mysql 计数不同的结果

sql - 在管道分隔串联期间格式化列标题 (Oracle SQL)

mysql - XAMPP MySQL 启动然后停止

sql - 如何通过 ODBC 从 Postgresql 链接到 SQL Server?

c# - 我可以使用 varbinary 类型在 SQL Server 数据库中存储图像吗?

sql - 选择以数字开头的值

mysql - InnoDB 是否缓存不存在的键?