2

First of all, I am very much at a "Beginner's level" so I'm not sure if what I'm asking for is possible but I will explain, to the best of my ability, and appreciate any help that's provided.

Below are two very basic tables to add a visual to my question. The first one just lists the test questions, which multiple choice answer is correct, and the possible answers. The second shows the questions, the correct multiple choice answer, the multiple choice answer selected, and the person that answered:

+---------------------------------------------------------------------------------+
|                                   Test Questions                                |
+-----------------------------+--------+----------+----------+----------+---------+
| id | QuestionText           | Answer | AnswerA  | AnswerB  | Answer C | Answer D|
+----+------------------------+--------+----------+----------+----------+---------+
| 1  | What Color is the Sky? |   C    | Red      | Green    | Blue     | Purple  |
+----+------------------------+--------+----------+----------+----------+---------+
| 2  | What has one wheel?    |   A    | Unicycle | Tricycle | Bike     | Car     |
+----+------------------------+--------+----------+----------+----------+---------+
| 3  | Who is the President?  |   B    | Bush     | Obama    | Clinton  | Kennedy |
+----+------------------------+--------+----------+----------+----------+---------+

+---------------------------------------------------------+
|                       Test Results                      |                        
+-----------------------------+--------+----------+-------+
| id | QuestionText           | Answer | Response | EE    |
+----+------------------------+--------+----------+-------+
|  1 | What Color is the Sky? |   C    |    A     | Bill  |
+----+------------------------+--------+----------+-------+
| 2  | What has one wheel?    |   A    |    A     | Bill  |
+----+------------------------+--------+----------+-------+
| 3  | Who is the President?  |   B    |    D     | Bill  |
+----+------------------------+--------+----------+-------+
| 4  | What Color is the Sky? |   C    |    C     | Susie |
+----+------------------------+--------+----------+-------+
| 5  | What has one wheel?    |   A    |    B     | Susie |
+----+------------------------+--------+----------+-------+
| 6  | Who is the President?  |   B    |    A     | Susie | 
+----+------------------------+--------+----------+-------+

After joining the tables, I want to combine the Answer column with the AnswerA column, AnswerB column...etc, based on the letter in the Answer column. Much like this:

+----------------------------------------------------------------+
|                         Test Results                           |
+-----------------------------+---------------+----------+-------+
| id | QuestionText           | CorrectAnswer | Response |   EE  |  
+----+------------------------+---------------+----------+-------+
| 1  | What Color is the Sky? |   C  Blue     |    A     | Bill  |
+----+------------------------+---------------+----------+-------+
| 2  | What has one wheel?    |   A  Unicycle |    A     | Bill  |
+----+------------------------+---------------+----------+-------+
| 3  | Who is the President?  |   B Obama     |    D     | Bill  | 
+----+------------------------+---------------+----------+-------+

Any suggestions?

1
  • 1
    what database is this? msSQL, MySQL, Oracle, and what version?
    – xQbert
    Commented Jun 13, 2014 at 20:09

2 Answers 2

1

Use CASE expression.

SELECT CASE Answer
       WHEN 'A' THEN AnswerA
       WHEN 'B' THEN AnswerB
       WHEN 'C' THEN AnswerC
                ELSE AnswerD
       END as CorrectAnswerText
FROM JoinedTable

Figuring out the syntax for concatenation is left as an exercise. :)

1
  • I bet he can figure out the syntax for concatenation if the idea is presented to him.
    – TMNT2014
    Commented Jun 13, 2014 at 20:21
1

You might consider reorganizing your tables rather than finding an answer to your question. With your current tables, you'll likely have an ongoing need to develop unique SQL queries, but I bet you'd rather not spend your day doing that.

Try giving each of your "things" (e.g. questions, answers, test takers, etc.) its own separate table, then string them together using primary and foreign keys. A more "normalized" table structure might save you from overly complicated SQL queries, and allow you to write queries that look something like this:

SELECT Questions.text, Answers.letter, Answers.text
FROM Answers
JOIN Questions ON Answers.question_id=Questions.id
WHERE Answers.correct=TRUE

Which would return results that look something this:

+--------------------------------------------------------+
|                     Test Results                       |
+------------------------+----------------+--------------+
| Question.text          | Answers.letter | Answers.text |
+------------------------+----------------+--------------+
| What Color is the Sky? |   C            |   Blue       |
+------------------------+----------------+--------------+
| What has one wheel?    |   A            |   Unicycle   |
+------------------------+----------------+--------------+
| Who is the President?  |   B            |   Obama      |
+------------------------+----------------+--------------+

See this article for more on data normalization: http://en.wikipedia.org/wiki/Database_normalization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.