sql – 仅基于表的一列消除重复值

前端之家收集整理的这篇文章主要介绍了sql – 仅基于表的一列消除重复值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的查询
  1. SELECT sites.siteName,sites.siteIP,history.date
  2. FROM sites INNER JOIN
  3. history ON sites.siteName = history.siteName
  4. ORDER BY siteName,date

第一部分输出

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

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

解决方法

这是窗口函数row_number()派上用场的地方:
  1. SELECT s.siteName,s.siteIP,h.date
  2. FROM sites s INNER JOIN
  3. (select h.*,row_number() over (partition by siteName order by date desc) as seqnum
  4. from history h
  5. ) h
  6. ON s.siteName = h.siteName and seqnum = 1
  7. ORDER BY s.siteName,h.date

猜你在找的MsSQL相关文章