Returning a unique set of values from a table

MrFlinstone 686 Reputation points
2021-01-19T14:38:45.793+00:00

Hi All.

I have a table log as per below.

58047-image.png

What I will like to do is return the last 3 release no's ordering by the release data.

In the case of the example below, that would be 77,66 and 55

The query below is not correct.

select releaseno  
from  
(select distinct releaseno, releasedate  
from release_log  
order by releasedate desc) last_3_release_no_extract  
  
Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-01-19T14:55:12.76+00:00

    Try this query too:

    select top(3) releaseno, max(releasedate) as releasedate
    from release_log
    group by releaseno
    order by releasedate desc
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Tom Cooper 8,481 Reputation points
    2021-01-19T14:49:34.583+00:00
    ;With cte As
    (Select releaseno, 
      Dense_Rank() Over(Order By releaseno Desc) As rk
    From release_log)
    Select releaseno
    From cte
    Where rk <= 3;
    

    Tom

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.