根据列值组合列

时间:2014-06-13 20:05:20

标签: sql

首先,我非常喜欢初级""因此,我不确定我所要求的是否可行,但我会尽我所能解释,并感谢所提供的任何帮助。

下面是两个非常基本的表格,用于为我的问题添加视觉效果。第一个只列出了测试题,多项选择答案是正确的,以及可能的答案。第二个显示问题,正确的多项选择答案,选择的多项选择答案以及回答的人:

+---------------------------------------------------------------------------------+
|                                   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 | 
+----+------------------------+--------+----------+-------+

加入表后,我想根据Answer列中的字母将Answer列与AnswerA列,AnswerB列等结合起来。很像这样:

+----------------------------------------------------------------+
|                         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  | 
+----+------------------------+---------------+----------+-------+

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

使用CASE表达式。

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

确定连接的语法是一个练习。 :)