Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>DECLARE @t Table(Name Varchar(30),[Year] Int, [Month] Int,Value Int) Insert Into @t Values('JERRY' , 2012, 9, 100 ) Insert Into @t Values('JERRY', 2012, 9 , 120) Insert Into @t Values('JERRY' , 2012, 9 , 130) Insert Into @t Values('JERRY', 2012 , 8 , 20) Insert Into @t Values('JERRY', 2011, 12 , 50) Declare @LatestYr Int Declare @LatestMonth Int Select @LatestYr= Max([Year])From @t Select @LatestMonth = Max([Month]) From @t Where [Year] = @LatestYr Select * From @t Where ([Year] = @LatestYr And [Month] = @LatestMonth) </code></pre> <p><strong>Result</strong></p> <p><img src="https://i.stack.imgur.com/sYNM5.png" alt="enter image description here"></p> <p>The above query will work just for a single user. And will fail for multiple users, or in case of ties. For example, consider the below scenario</p> <p><img src="https://i.stack.imgur.com/joLJn.png" alt="enter image description here"></p> <p>In this case, the needed output will be</p> <p><img src="https://i.stack.imgur.com/s9QKq.png" alt="enter image description here"></p> <p>So for handling such a situation, I am proposing the below solutions</p> <p><strong>Solution 1</strong></p> <pre><code>Select t.* From @t t Join ( Select x.Name,x.Max_Year,y.Max_Month From ( SELECT Name,Max_Year = Max([Year]) From @t Group By Name )x Join ( SELECT Name,[Year],Max_Month= Max([Month]) From @t Group By Name,[Year] )y On x.Name = y.Name And x.Max_Year = y.[Year] )x On t.Name = x.Name And t.[Year] = x.Max_Year And t.[Month] = x.Max_Month </code></pre> <p>OR</p> <p><strong>Solution 2 (Sql Server 2005+)</strong></p> <pre><code>Select Name,[Year],[Month],Value From ( Select *,Rn = Rank() Over(Partition By Name Order By [Year] desc, [Month] Desc) From @t )X Where X.Rn =1 </code></pre> <p><strong>Solution 3 (Sql Server 2005+)</strong></p> <pre><code>Select Name,[Year],[Month],Value From ( Select *,Rn = Dense_Rank() Over(Partition By Name Order By [Year] desc, [Month] Desc) From @t )X Where X.Rn =1 </code></pre> <p>The ddl is as under</p> <pre><code>DECLARE @t Table(Name Varchar(30),[Year] Int, [Month] Int,Value Int) Insert Into @t Values('JERRY' , 2012, 9, 100 ) Insert Into @t Values('JERRY', 2012, 9 , 120) Insert Into @t Values('JERRY' , 2012, 9 , 130) Insert Into @t Values('JERRY', 2012 , 8 , 20) Insert Into @t Values('JERRY', 2011, 12 , 50) Insert Into @t Values('FERRY' , 2010, 9, 100 ) Insert Into @t Values('FERRY', 2010, 9 , 120) Insert Into @t Values('FERRY', 2010, 8 , 120) Insert Into @t Values('JERRY1' , 2012, 9, 100 ) Insert Into @t Values('JERRY1', 2012, 9 , 120) Insert Into @t Values('JERRY1' , 2012, 9 , 130) Insert Into @t Values('JERRY1', 2012 , 8 , 20) Insert Into @t Values('JERRY1', 2011, 12 , 50) </code></pre> <p>Hope this may help. Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload