Skip to content
Advertisement

Randomly select orders per customer from database

DB-Fiddle

Exptected Result:


I have a huge database and need to extract some data from it for a case study.
The problem is that I need to extract the full year of the data because I want to be able to conduct a monthly analysis in the case study. Therefore, I can not limit the extract with dates or LIMIT.

Thus, my idea to solve this issue is a query which extracts randomly maximal two orders per customer per month.

Do you have any idea if this is possible?
If yes, how do I need to modify the below query?

Advertisement

Answer

Thus, my idea to solve this issue is a query which extracts randomly maximal two orders per customer per month.

You can use:

To be honest, though, for analytic purposes, I would prefer to take a random sample of customers — say 1% or 5% — and all the transactions for them.

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