This can be solved in two stages. I will describe how to do this for an example where gender and region are dimensions. Then I will describe a more general case. At the first stage, we solve a system of equations of 8 variables, then we take a disconnected union of 8 selection operators, limited by the solutions found in the first step. Please note that there are only 8 possibilities for any row. They can be men or women, and then the region is one of the northern, southern, eastern or western. Let now
X1 equal the number of rows that are male and from the north, X2 equal the number of rows that are male and from the south, X3 equal the number of rows that are male and from the east, X4 equal then number that are male and from the west X5 equal the number of rows that are female and from the north, X6 equal the number of rows that are female and from the south, X7 equal the number of rows that are female and from the east, X8 equal then number that are female and from the west
Equations
X1+X2+X3+X4=600 X5+X6+X7+X8=400 X1+X5=100 X2+X6=200 X3+X7=300 X4+X8=400
Now decide for X1, X2, ... X8 in the above. There are many solutions (I will tell you how to solve in an instant). Here is the solution:
X1=60, X2=120, X3=180,X4=240,X5=40,X6=80,X7=120,X8=160.
Now we can get the result by a simple connection of 8 elements:
(select * from user where gender='m' and region="north" limit 60) union distinct(select * from user where gender='m' and region='south' limit 120) union distinct(select * from user where gender='m' and region='east' limit 180) union distinct(select * from user where gender='m' and region='west' limit 240) union distinct(select * from user where gender='f' and region='north' limit 40) union distinct(select * from user where gender='f' and region='south' limit 80) union distinct(select * from user where gender='f' and region='east' limit 120) union distinct(select * from user where gender='f' and region='west' limit 160);
Please note that if the database does not contain 60 rows, satisfy the first one selected above, then the specific given solution will not work. Therefore, we need to add other restrictions, LT:
0<X1 <= (select count(*) from user where from user where gender='m' and region="north") 0<X2 <= (select count(*) from user where gender='m' and region='south') 0<X3 <= (select count(*) from user where gender='m' and region='east' ) 0<X4 <= (select count(*) from user where gender='m' and region='west') 0<X5 <= (select count(*) from user where gender='f' and region='north' ) 0<X6 <= (select count(*) from user where gender='f' and region='south') 0<X7 <= (select count(*) from user where gender='f' and region='east' ) 0<X8 <= (select count(*) from user where gender='f' and region='west');
Now let's summarize the permissible splits for this case. Equations E:
X1+X2+X3+X4=n1 X5+X6+X7+X8=n2 X1+X5=m1 X2+X6=m2 X3+X7=m3 X4+X8=m4
The numbers n1, n2, m1, m2, m3, m4 are given and satisfy n1 + n2 = (m1 + m2 + m3 + m4). Thus, we have led the problem to solving the equations of LT and E above. This is simply a linear programming problem and can be solved using the simplex method or other methods. Another possibility is to consider this as a System of linear Diophantine equations and use the methods for this to find solutions. In any case, I solved the problem of finding a solution to the above equations. (Given that the equations have a special form, there may be a faster way than using the simplex method or solving a system of linear Diophantine equations). Once we decide for Xi the final solution:
(select * from user where gender='m' and region="north" limit :X1) union distinct(select * from user where gender='m' and region='south' limit :X2) union distinct(select * from user where gender='m' and region='east' limit :X3) union distinct(select * from user where gender='m' and region='west' limit :X4) union distinct(select * from user where gender='f' and region='north' limit :X5) union distinct(select * from user where gender='f' and region='south' limit :X6) union distinct(select * from user where gender='f' and region='east' limit :X7) union distinct(select * from user where gender='f' and region='west' limit :X8);
Denote the dimension D with n possibilities by D: n. Suppose you have dimensions D1: n1, D2: n2, ... DM: nM. It will generate the variables n1 * n2 * ... nM. The number of generated equations is n1 + n2 + ... nM. Instead, we define a general method to take another case of three dimensions, 4 dimensions and 2 dimensions; Allows you to call the possible values for D1 equal to d11, d12, d13, D2 - the values of d21, d22, d23, d24 and D3 are equal to d31, d32. We will have 24 variables, and the equations:
X1 + X2 + ...X8=n11 X9 + X10 + ..X16=n12 X17+X18 + ...X24=n13 X1+X2+X9+x10+x17+x18=n21 X3+X4+X11+x12+x19+x20=n22 X5+X6+X13+x14+x21+x22=n23 X7+X8+X15+x116+x23+x24=n24 X1+X3+X5+...X23=n31 X2+X4+......X24=n32
Where
X1 equals number with D1=d11 and D2=d21 and D3=d31 X2 equals number with D1=d11 and D2=d21 and D3 = d31 .... X24 equals number with D1=D13 and D2=d24, and D3=d32.
Add fewer restrictions. Then decide for X1, X2, ... X24. Create 24 select statements and take a disconnected union. We can solve similarly for any measurements.
So, in short: given the sizes D1: n1, D2: n2, ... DM: nM, we can solve the corresponding linear programming problem, as described above for n1 * n2 * ... nM variables, and then generate the solution by taking a disconnected union over the operators n1 * n2 * ... nM select. So, yes, we can generate a solution using select statements, but first we need to solve the equations and determine the limits, getting the calculations for each of the variables n1 * n2 * ... nM.
Despite the fact that the generosity is over, I'm going to add a little more for those you are interested in. I affirm that I have fully shown how to solve this, if there is a solution.
To clarify my approach. In the case of 3 measurements, let's say we divided the age into one of three possibilities. Then make good use of gender and region, as in the question. For each user, there are 24 different options corresponding to where they fall into these categories. Let Xi be the number of each of these possibilities in the final result. Let me write a matrix where each row represents one of the possibilities. Each user will contribute a maximum of 1 to m or f, 1 to the north, south, east or west, and 1 to the age category. And there are only 24 possibilities for the user. Let's show the matrix: (abc) 3 ages, (nsew) regions and (mf) male or female: a age less than or equal to 10, b - age from 11 to 30 years, c - age from 31 to 50 years.
abc nsew mf X1 100 1000 10 X2 100 1000 01 X3 100 0100 10 X4 100 0100 01 X5 100 0010 10 X6 100 0010 01 X7 100 0001 10 X8 100 0001 01 X9 010 1000 10 X10 010 1000 01 X11 010 0100 10 X12 010 0100 01 X13 010 0010 10 X14 010 0010 01 X15 010 0001 10 X16 010 0001 01 X17 001 1000 10 X18 001 1000 01 X19 001 0100 10 X20 001 0100 01 X21 001 0010 10 X22 001 0010 01 X23 001 0001 10 X24 001 0001 01
Each row represents a user in which there is 1 in the column if he contributes to the result. For example, the first line shows 1 for a, 1 for n, and 1 for m. This means that the user's age is less than or equal to 10, from the north and is a man. Xi represents how many of this number of rows are in the final result. So, let's say that X1 is 10, which means that we say that the end result has 10 results, all of which are from the north, are males and less than or equal to 10. Okay, now we just need to add something. Please note that the first 8 X1+X2+X3+X4+X5+X6+X7+X8 are all lines whose age is less than or equal to 10. They must correspond to all that we have chosen for this category. Similarly for the next 2 sets of 8.
So, until now we get the equations: (na is a number with an age of less than 10, nb is an age from 10 to 20, nc is a number whose age is less than 50
X1+X2+X3+X4+X5+X6+X7+X8 = na X9+X10+X11 + .... X16 = nb X17+X18+X19+... X24=nc
These are age-related cleavages. Now let's look at the division of the area. Just add the variables to the "n" column,
X1+X2+X9+X10+X17+X18 = nn X3+X4+X11+X12+X19+20=ns ...
etc .. you see how i get these equations just by looking down the columns? We continue for ew and mf. giving 3 + 4 + 2 equations in general. So what I did here is pretty simple. I reasoned that any row you select contributes to each of the three dimensions, and there are only 24 possibilities. Then let Xi be the number for each opportunity, and you will get the equations that need to be solved. It seems to me that any method that you came up with should be a solution to these equations. In other words, I simply reformulated the problem in terms of solving these equations.
Now we need an integer solution, since we cannot have a fractional string. Please note that these are all linear equations. But we want an integer solution. Here is a link to an article that describes how to solve them: https://www.math.uwaterloo.ca/~wgilbert/Research/GilbertPathria.pdf