Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"global" calculations for sorted sets

Tags:

redis

With a sorted set, is there an easy way to compute "global" values, for instance the average, median, max, min, etc. of all the scores in the set, or do I always need to extract the scores and do the calculations myself (which is a bit of a pain ...)?

like image 705
Running Turtle Avatar asked Nov 14 '11 09:11

Running Turtle


1 Answers

You can do it all with ZCARD and ZRANGE (withscore of course).

  • Min: ZRANGE on index 0
  • Max: ZRANGE on max index (ZCARD value)
  • Median: ZRANGE on the middle index (ZCARD/2)
  • Average: you can store the total off all score in another counter and then divide it by the ZCARD value
like image 118
plus- Avatar answered Sep 23 '22 13:09

plus-