Skip to content
Advertisement

How can I apply mutliple filters on my sql tables

I am struggling with what looks to be relational division in sql. I need to filter data from the same table based on multiple criteria. Here-under is a schema of what my table looks like.

I need the RespondenIds from this table called Answers that match the following filters : Question = Big and Children and Answer = Yes and Yes respectively for every question. Therefore, if I would have a correct Sql query my result should return me the following array : [2] since the only row that has the answer Yes for the question Big and the answer Yes for the question Children is the one with RespondentId = 2.

Also, the questions and the answers provided are not fixed and should be modular. For instance, I should be able to change an answer or removing a question without having to change the whole structure of my query.

Could you please help me finding a correct query for this problem ? I have been looking to a lot of explanations provided by @Erwin Brandstetter but none of them match my needs.

Advertisement

Answer

I would do this as:

This is easily generalized to:

This is pretty generalizable. You could even arrange the CTE to take an array or json argument and parse out into the separate comparison values.

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