I'm having a difficult time trying to do a query to display results in a certain way. What I need is to display all user answers in a single row per user, I cannot concatenate all the answers because my program needs to access the result of every answer and for what I understand concatenation will return a string.
This is how the tables are defined:
Here's where a user's information is stored. Users
+------+---------+-------------+
| u_id | email | name |
+------+---------+-------------+
| 1 | bob@b | bob |
| 2 | [email protected] | sam |
| 3 | ra@se | steve |
| 4 | tv@we | rob |
| 5 | tr@sd | ted |
| 6 | qw@as | john |
+------+---------+-------------+
Questions
+------+---------+-------------+
| q_id | question_name |
+------+---------+-------------+
| 1 | Age range? |
| 2 | Do you use Amazon? |
| 3 | Do you use Facebook? |
| 4 | Interested in toys? |
+------+---------+-------------+
These are the possible answers a user can choose.
Option_Choices
+------+---------+-------------+
|oc_id | opt_choice_name |
+------+---------+-------------+
| 1 | YES |
| 2 | NO |
| 3 | 18-24 Years |
| 4 | 25-35 Years |
| 5 | Very Interested |
| 6 | Not Interested |
+------+---------+-------------+
Here's where a question is related with every possible answer for that specific question.
Question_Options
+------+---------+-------------+
|qo_id | q_id | oc_id |
+------+---------+-------------+
| 1 | 1 | 3 | // Age range?: 18-24
| 2 | 1 | 4 | // Age range?: 25-35
| 3 | 2 | 1 | // Do you use Amz? Yes
| 4 | 2 | 2 | ...
| 5 | 3 | 1 | ...
| 6 | 3 | 2 | ...
| 7 | 4 | 5 | ...
| 8 | 4 | 6 |
+------+---------+-------------+
Here's where the answers given by every user are stored.
Answers
+------+---------+-------------+
| a_id | u_id | qo_id |
+------+---------+-------------+
| 1 | 1 | 2 |
| 2 | 1 | 4 |
| 3 | 1 | 6 |
| 4 | 1 | 7 |
| 1 | 2 | 1 |
| 2 | 2 | 3 |
| 3 | 2 | 6 |
| 4 | 2 | 8 |
| 1 | 3 | 2 |
| 2 | 3 | 4 |
| 3 | 3 | 5 |
| 4 | 3 | 8 |
+------+---------+-------------+
What I need now is to build a query to display the information in a similar manner as this:
+---------+-------------+-------------+-------------+---------------------+
| email | Age range? | Use Amazon? | Use FB? | Interested in toys? |
+---------+-------------+-------------+-------------+---------------------+
| bob@b | 18-24 Years | YES | NO | Very Interested |
| [email protected] | 25-35 Years | NO | YES | Not Interested |
| ra@se | 25-35 Years | NO | YES | Not Interested |
| tv@we | 18-24 Years | YES | YES | Not Interested |
| tr@sd | 18-24 Years | YES | NO | Not Interested |
| qw@as | 25-35 Years | YES | YES | Very Interested |
+------+---------+--------------------+-------------+---------------------+
But using just JOIN I get something like this"
+---------+----------------+-----------------+
| email | question_name | opt_choice_name |
+---------+----------------+-----------------+
| bob@b | Age range? | 18-24 Years |
| bob@b | Use Amazon? | YES |
| bob@b | Use FB? | NO |
| bob@b | Inter in toys? |Very Interested |
| [email protected] | Age range? | 25-35 Years |
| [email protected] | Use Amazon? | NO |
| [email protected] | Use FB? | YES |
| [email protected] | Inter in toys? | Not Interested |
... ... ...
+---------+----------------+-----------------+
I tried using pivots with a query like this:
SELECT u.email,
(CASE q.question_name WHEN q_id = 1 THEN oc.opt_choice_name ELSE 0) AS 'Age_Range',
(CASE q.question_name WHEN q_id = 2 THEN oc.opt_choice_name ELSE 0) AS 'Amazon',
(CASE q.question_name WHEN q_id = 3 THEN oc.opt_choice_name ELSE 0) AS 'FB',
(CASE q.question_name WHEN q_id = 4 THEN oc.opt_choice_name ELSE 0) AS 'Toys'
FROM Users u
INNER JOIN Ansers a ON u.u_id = a.u_id
INNER JOIN Question_Options qo ON a.qo_id = qo.qo_id
INNER JOIN Questions q ON qo.q_id = q.q_id
INNER JOIN Option_Choices oc ON qo.oc_id = oc.oc_id
GROUP BY u.email
But I'm getting mixed results.
Any suggestion will be greatly appreciated.
Thanks.
I tried using MAX
SELECT u.email,
MAX( CASE q.question_name WHEN q.question_id = 1 THEN oc.option_choice_name ELSE 0 END) AS 'AgeRange',
MAX( CASE q.question_name WHEN q.question_id = 2 THEN oc.option_choice_name ELSE 0 END) AS 'Amazon',
MAX( CASE q.question_name WHEN q.question_id = 3 THEN oc.option_choice_name ELSE 0 END) AS 'FB',
MAX( CASE q.question_name WHEN q.question_id = 4 THEN oc.option_choice_name ELSE 0 END) AS 'toys',
FROM Users u
INNER JOIN Answers a ON u.u_id = a.u_id
INNER JOIN Question_Options qo ON a.qo_id = qo.qo_id
INNER JOIN Questions q ON qo.q_id = q.q_id
INNER JOIN Option_Choices oc ON qo.oc_id = oc.oc_id
GROUP BY u.email
But the results I'm getting are like:
+---------+-------------+-------------+-------------+---------------------+
| email | Age range? | Use Amazon? | Use FB? | Interested in toys? |
+---------+-------------+-------------+-------------+---------------------+
| bob@b | YES | YES | NO | NO |
| [email protected] | YES | YES | YES | YES |
| ra@se | YES | YES | YES | YES |
| tv@we | YES | YES | YES | YES |
+------+---------+--------------------+-------------+---------------------+