Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I filter the result of a query based on restrictions
    primarykey
    data
    text
    <p>I have a problem with a dynamically generated SQL query. I have a HUGE database where users are stored in different tables. I need to dynamically create a query which will return me the user_id-s based on some restrictions with different partitioning. So far i created a logic which will create me the query based on the restrictions, but I can't figure it out how could I filter the result to have different partitioning. For example:</p> <p><b>I have the following:</b></p> <p>1: Male<br> 2: Female<br> 3: Age between 18-24<br> 4: Age between 25-99<br> 5: Working at IBM<br> Lets say i have 50k female, 50k male</p> <p>Now if my restriction is (1 or 2) and 5, which translates to if it's a male or female working at IBM, I generated something like this</p> <pre><code>SELECT DISTINCT user_id from usertable1 where age in (0,1) INTERSECT SELECT DISTINCT user_id from usertable2 where work_place = 'IBM' </code></pre> <p>Now my problem is that this isn't enough, there are times when i have to partition the resulting set of user ids like so for example that I have 33% male and 66% female in the result list. I don't know how to get it properly. I tried to count how many female and male user I have, then apply a top to them, like so:</p> <pre><code>SELECT DISTINCT top 25000 user_id from usertable1 where age in (0) UNION SELECT DISTINCT top 50000 user_id from usertable1 where age in (1) SELECT DISTINCT user_id from usertable2 where work_place = 'IBM' </code></pre> <p>However this isn't giving me the right solution, the problem with this is that if I have no female user working at IBM in my list this will return 25k male and 0 female, which is not 33% male and 66% female... </p> <p>Now to complicate it even more, I can have even harder partitioning like so that the result must contain 33% male from which 75&amp; percent is age between 18-24 and 25&amp; age between 25-99 and 66% female from which 75&amp; percent is age between 18-24 and 25&amp; age </p> <p><b>So my problem is that i have no idea how to solve both of the problem, which can work on huge database, 50k male and 50k female is small compared to the full database. Anyone have any idea for at least one of my problem?</b></p> <p><b>EDIT 1:</b> My table has a bit strange structure, it's more like a link table. To simplify it and to show my problem, lets consider this example table which has similar format of mine. I have user_id, question, question_answer, age, gender. The same user_id appears multiple time as this is only a link table and a user mostly answers for multiple question. The structure of this is by far not optimal i know, but it wasn't designed by me, nor can be modified by me. My task is to get the distinct user_id's which completes all the restriction and filter the result to have percentage partitioning based on another restriction set. So far I got the correct user_id's based on the restriction, but i have no idea how can i modify this to be able have percentage partitioning.</p> <p>So, to be more precise, i have a restriction set which can be something like (1 OR 2) AND 3 AND 4 so on. I create a reverse polish form from this to be able to solve it in order. Based on every restriction (for example be male, age between 18-24) I create quires which I then concatenate with union or intersect. This work well as long as i don't need to partition it. But unfortunately that's the case and because my result doesn't contain any detail about the answered questions, nor age nor gender only the user_id, i have no idea how could i modify it, to be able to filter the result to have the wanted partition sizes by percentage. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload