SQL Select Distinct column and latest date

后端 未结 2 626
天命终不由人
天命终不由人 2021-02-08 19:01

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;



        
相关标签:
2条回答
  • 2021-02-08 19:36

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

    select URL, max(DateVisited)
    from <table>
    group by URL
    
    0 讨论(0)
  • 2021-02-08 19:39

    This is usually done using row_number():

    select t.*
    from (select t.*,
                 row_number() over (partition by url order by datevisited desc) as seqnum
          from t
         ) t
    where seqnum = 1;
    

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

    0 讨论(0)
提交回复
热议问题