Calculate rank so that the smaller number is higher rank

JR_S1L Member
edited March 2023 in SQL DataFlows

I need to calculate the rank on a metric where the lower value is higher in rank, and the higher values are lower in rank. This is how I've been calculating standard rank (where higher values are high in rank)

SELECT `Metric`







WHEN @priorMetric = `MetricID` AND `VALUE` = @priorValue THEN @rank := @rank

  WHEN @priorMetric = `MetricID` THEN @rank := @rank + 1 

  else @rank := 1

  END AS Rank 

,@priorMetric := `MetricID` as MetricID  

,@priorValue := `VALUE`

FROM sort

JOIN(SELECT @rank := 0, @priorMetric:= '', @priorValue := 0) r 



  • GrantSmith

    How is your sort table defined? Can you just make sure you're sorting the values in ASC instead of DESC?

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**