SQL Select Distinct列和最新日期 [英] SQL Select Distinct column and latest date

查看:178
本文介绍了SQL Select Distinct列和最新日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望仅根据日期选择表的最新记录,但每个网址仅列出一个。表格结构如下;

I'm looking to select just the latest records of a table based on date, but only one one Distinct listing of each of the urls. The table structure is like this;

ID        URL          DateVisited
1         google.com   01-01-2016
2         yahoo.com    01-02-2016
3         google.com   12-30-2015
4         google.com   02-01-2016

所以对于我的结果集,我想要

So for my result set I would want

google.com     02-01-2016
yahoo.com      01-02-2016

我将有一个在我的实际查询中添加了更多条件,但只想获取命中日志中的最新记录,而不是唯一的URL和日期列表,只是唯一的URL和最新日期。

I will have a couple more conditionals in my actual query, but just want to get the single latest records in a hit log, rather than list of distinct urls and dates, just distinct url's and the latest date.

推荐答案

使用简单的聚合实际上很容易做到,就像这样:

This is actually pretty easy to do using simple aggregation, like so:

select URL, max(DateVisited)
from <table>
group by URL

这篇关于SQL Select Distinct列和最新日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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