生成所需的输出

时间:2013-06-25 08:42:53

标签: sql-server tsql

我在下面给出了第一种格式的表格。我想要第二种格式的输出。感谢帮助。谢谢!

UserID  IsManagerFeedback   Level
---------------------------------
User 1  FALSE                2
User 2  FALSE                3
User 5  FALSE                2
User 1  TRUE                 4
User 3  TRUE                 1
User 4  TRUE                 3
User 5  TRUE                 5




UserID  UserFeedback    ManagerFeedback
---------------------------------------
User 1  2               4
User 2  3               NA
User 3  NA              1
User 4  NA              3
User 5  2               5

1 个答案:

答案 0 :(得分:1)

SELECT
UserID,
MAX(CASE WHEN IsManagerFeedback = 'FALSE' THEN Level END) AS UserFeedback,
MAX(CASE WHEN IsManagerFeedback = 'TRUE' THEN Level END) AS ManagerFeedback
FROM
Table1
GROUP BY UserID

看到它在sqlfiddle中正常工作。

更新:

SELECT
Year,
UserID,
MAX(CASE WHEN IsManagerFeedback = 'FALSE' THEN Level END) AS UserFeedback,
MAX(CASE WHEN IsManagerFeedback = 'TRUE' THEN Level END) AS ManagerFeedback
FROM
Table1
GROUP BY Year, UserID