DixonMD
DixonMD

Reputation: 93

SQL SELECT Query based on yes or no questions

I have two question sets. One is Yes or No questions. Based on this question's answers I need to display another set of questions. For this I have added a question code for Yes or No questions. and for the second set questions given these question codes to identify for displaying this question or not.

For example 
Y/N questions - Question Code
1.AAAAAAAA - AAA
2.BBBBBBBB - BBB
3.CCCCCCCC - CCC

Second Set questions - Question Code
1.XXXXXXXX - AAA && BBB
2.YYYYYYYY - AAA && !BBB
3.ZZZZZZZZ - AAA && !BBB && CCC

So second set question number 1 will display only if Y/N question number 1 and 2 answer Yes. Question number 2 will display only if Y/N question number 1 answer Yes and number 2 answer No. Question number 3 will display only if Y/N question number 1 answer Yes, number 2 answer No and number 3 answer Yes.

How can we write query for displaying second set of questions to display based on the Y/N question answers.

Upvotes: 0

Views: 739

Answers (2)

John
John

Reputation: 721

Another approach would be to normalize your tables. Your 2nd table's 2nd column should really be an associative (many-to-many) table between the first and second set of questions. With that in place your query would be much simpler.

Upvotes: 1

Lukas Eichler
Lukas Eichler

Reputation: 5913

Putting this logic inside the database is in my opinion a bad design approach. Instead I would query the results to your logic layer and analyse the results. From these results you could create a new query with the next questions.

With all the logic inside your query statement it makes it very hard to maintain.

Upvotes: 0

Related Questions