Monday, 19 August 2013

How to select different percentages of data based in a column value?

How to select different percentages of data based in a column value?

I need to query a table that have a "gender" column, like so:
| id | gender | name |
-------------------------
| 1 | M | Michael |
-------------------------
| 2 | F | Hanna |
-------------------------
| 3 | M | Louie |
-------------------------
And I need to extract the first N results which have, for example 80%
males and 20% females. So, if I needed 1000 results I would want to
retrieve 800 males and 200 females.
Is it possible to do it in a single query? How?
If I don't have enough records (imagine I have only 700 males on the
example above) is it possible to select 700 / 300 automatically?

No comments:

Post a Comment