Skip to content Skip to sidebar Skip to footer

Sql Select Distinct Column And Latest Date

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

Solution 1:

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

select URL, max(DateVisited)
from<table>groupby URL

Solution 2:

This is usually done using row_number():

select t.*from (select t.*,
             row_number() over (partitionby url orderby datevisited desc) as seqnum
      from t
     ) t
where seqnum =1;

This allows you to get all the columns associated with the latest record.

Post a Comment for "Sql Select Distinct Column And Latest Date"