1

我正在尝试使用 transpose/pivot (SQL Server 2008),但是 MAX 函数和 TRANSFORM 都不适合我。有没有办法以不同的方式实现这一目标,或者使用 MSEXCEL?

这就是我所拥有的

`

<table>
<tr><td>Name</td><td>Question</td><td>Answer</td></tr>
<tr><td>Respondent 1</td><td>Question 1</td><td>Answer</td></tr>
<tr><td>Respondent 1</td><td>Question 2</td><td>Answer</td></tr>
<tr><td>Respondent 1</td><td>Question 3</td><td>Answer</td></tr>
<tr><td>Respondent 1</td><td>Question 4</td><td>Answer</td></tr>
<tr><td>Respondent 1</td><td>Question 5</td><td>Answer</td></tr>
<tr><td>Respondent 2</td><td>Question 1</td><td>Answer</td></tr>
<tr><td>Respondent 2</td><td>Question 2</td><td>Answer</td></tr>
<tr><td>Respondent 2</td><td>Question 3</td><td>Answer</td></tr>
<tr><td>Respondent 2</td><td>Question 4</td><td>Answer</td></tr>
<tr><td>Respondent 2</td><td>Question 5</td><td>Answer</td></tr>
<tr><td>Respondent 3</td><td>Question 1</td><td>Answer</td></tr>
<tr><td>Respondent 3</td><td>Question 2</td><td>Answer</td></tr>
<tr><td>Respondent 3</td><td>Question 3</td><td>Answer</td></tr>
<tr><td>Respondent 3</td><td>Question 4</td><td>Answer</td></tr>
<tr><td>Respondent 3</td><td>Question 5</td><td>Answer</td></tr>
<tr><td></td></tr>
</table>

`

我想结束以下内容:

`

<table>
<tr><td>Name</td><td>Question 1</td><td>Question 2</td><td>Question 3</td><td>Question 4</td><td>Question 5</td></tr>
<tr><td>Respondent 1</td><td>text</td><td>text</td><td>text</td><td>text</td><td>text</td></tr>
<tr><td>Respondent 2</td><td>text</td><td>text</td><td>text</td><td>text</td><td>text</td></tr>
<tr><td>Respondent 3</td><td>text</td><td>text</td><td>text</td><td>text</td><td>text</td></tr>
</table>

`

这是我尝试并失败的查询: SELECT Respondent, MAX(CASE WHEN Question='Question 1' THEN Answer ELSE NULL END) AS 'Question 1', MAX(CASE WHEN Question='Question 2' THEN Answer ELSE NULL END ) 作为“问题 2”来自表 GROUP BY 受访者

我认为 Excel 可能是最好的解决方案,有什么想法吗?

4

1 回答 1

1

您可以在 sql 中使用数据透视表:

Select * from tablename
Pivot( max(Answer) for Question in ([Question1], [Question2],[Question3],[Question4],[Question5])) as pivotTable
于 2013-11-08T19:33:22.133 回答