anderlaini
anderlaini

Reputation: 1831

how to calculate 'better than X% of users' in a column?

I have a table just like this:

|   id  |   totalScore   |
--------------------------
|   1   |       -3       |
|   2   |        6       |
|   3   |       10       |
|   4   |        7       |
|   5   |       -4       |
|   6   |        4       |
|   7   |       -3       |
...

So for a given number (example 5), I want to find * You're better than 10% of users! *, or 20%, or 30%, etc.

How can I do this? Maybe using average of totalScore column and group by 10, 20, 30 values? Is it possible?

I'm using Laravel 5.6.

Upvotes: 1

Views: 65

Answers (1)

PtrTon
PtrTon

Reputation: 3855

  1. Get the score for the user (5 in your case)
  2. Count all users where score is lower than 5
  3. Divide that amount by the total amount of users and times a 100 for a percentage

E.g. you'll have a user with score 5. You have a total of 7 users out of which 4 have a lower score than 5, resulting in 4/7*100 = 57%

Upvotes: 4

Related Questions