仅基于表的一列消除重复值 [英] Eliminating duplicate values based on only one column of the table

查看:79
本文介绍了仅基于表的一列消除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的查询:

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

输出的第一部分:

如何删除 siteName 列中的重复项?我只想根据日期列留下更新的一个。

How can I remove the duplicates in siteName column? I want to leave only the updated one based on date column.

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

In the example output above, I need the rows 1, 3, 6, 10

推荐答案

这是窗口函数 row_number() code>派上用场:

This is where the window function row_number() comes in handy:

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

这篇关于仅基于表的一列消除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆