让我说我有:
User Email
1 abc@email.com
1 efg@email.com
1 xyz@email.com
2 aaa@email.com
2 bbb@email.com
3 ccc@email.com
我想要输出:
User Eamil1 Email2 Email3
1 abc@email.com efg@email.com xyz@email.com
2 aaa@email.com bbb@email.com
3 ccc@email.com
我找到了关于使用FOR XML Path的其他答案,但这不是我想要的。它将行组合成一列,并使用';'
分隔值我也试过自己加入表格,这也不是我想要的结果。
感谢任何帮助。
答案 0 :(得分:2)
尝试这样的事情。在这种情况下,枢轴不是一个好的解决方案。对于非数值,Pivot效率不高。
WITH C AS(
SELECT
RANK() OVER (PARTITION BY [User] ORDER BY Email) AS [Rank]
,[User], Email
FROM Emails
)
SELECT [User]
,MIN(CASE C.[Rank] WHEN 1 THEN Email END) AS [Email1]
,MIN(CASE C.[Rank] WHEN 2 THEN Email END) AS [Email2]
,MIN(CASE C.[Rank] WHEN 3 THEN Email END) AS [Email3]
FROM C
GROUP BY [User]
答案 1 :(得分:0)