Good night gentlemen. There was a need to solve the problem of uniformity of sampling data from the database. As a more understandable example: You need to select 120 records with a weight of 10 to 12 kg, and the material should be, for example, plastic or metal. And it seems that there should just be something like:
select obj.* from objects obj where obj.weight >= 10 and obj.weight <= 12 and (obj.material = 'пластик' or obj.material = 'металл' ) limit 120;
But the problem is that such a request is likely to give me 120 records with a weight of 10 kg and material - metal, which is not suitable in my situation.
We need exactly a uniform sample: 20 plastic 10 kg each, 20 plastic 11 kg each, 20 plastic 12 kg each, 20 metal 10 kg each, and further by analogy. Total in the amount of exactly 120 required.
Were thought of a cycle to go through all the conditions, build queries for each of the above parts and get a number of resulting performance problems and sample adjustment in case of a lack of records in one of the groups of conditions.
Maybe I reinvent the wheel and there is a ready-made solution to my problem, or is there a more optimal solution direction? I work with PostgreSQL.