Search code examples
sql-serverssms

Get Overall Rank


I run the below sql server script to retrieve some data , but I need to add column to tell me in each LEAGUE which user is first, second and so on

Select 

(Select Name From League Where ID = League_Details.League_ID) As League
,Player
,(Select Total From AllUserPoints Where User_ID = Player) As Total


 From 
     League_Details

Where 
     LeagueType = 'Private'

order by League

this is the result:

enter image description here


Solution

  • You have to use order by properly in this statement. Try this one:

    Select (Select Name From League Where ID = League_Details.League_ID) As League
    ,Player, (Select Total From AllUserPoints Where User_ID = Player) As Total
    
     From 
         League_Details
    
    Where 
         LeagueType = 'Private'
    
    order by League, Total desc
    

    Try this and Let me know whether it works on not.