Skip to content
Advertisement

How I can select random amount of rows based on varios criterias?

I have a table that has 3 main categories: age, city and gender.

The gender column is boolean.

The age category is in the following range:

The city category has around 19 cities. So, for each city in category '18-25' and '75+' I would like to select 24 random rows for females and 20 for males.

For the rest of the cities, I would like to select 40 random males and 47 random females.

Is it possible to achieve this without having to run a specific query for each case?

I’m sorry the lack of code, but honestly I don’t even know where or how to start 🙁

Advertisement

Answer

You can use row_number() to enumerate the rows randomly and then just filter:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement