【发布时间】:2020-05-14 00:29:20
【问题描述】:
我有一个如下所示的源表:
╔════════════════════╦════════════════╦══════════════╦══════════════════╗
║ Topic ║ Person ║ PersonsReply ║ PersonsComment ║
╠════════════════════╬════════════════╬══════════════╬══════════════════╣
║ Is the earth flat? ║ This Person ║ Yes ║ It's flat. ║
║ Is the earth flat? ║ That Person ║ No ║ It's round ║
║ Is the earth flat? ║ Another Person ║ Maybe ║ Don't care. ║
╚════════════════════╩════════════════╩══════════════╩══════════════════╝
但从我在网上看到的示例来看,我似乎无法将我的数据转换为如下表格表示:
╔════════════════════╦══════════════════╦══════════════════╦═════════════════════╦════════════════════╦════════════════════╦═══════════════════════╗
║ Topic ║ ThisPersonsReply ║ ThatPersonsReply ║ AnotherPersonsReply ║ ThisPersonsComment ║ ThatPersonsComment ║ AnotherPersonsComment ║
╠════════════════════╬══════════════════╬══════════════════╬═════════════════════╬════════════════════╬════════════════════╬═══════════════════════╣
║ Is the earth flat? ║ Yes ║ No ║ Maybe ║ It's flat ║ It's round ║ Don't care ║
╚════════════════════╩══════════════════╩══════════════════╩═════════════════════╩════════════════════╩════════════════════╩═══════════════════════╝
如何使用 SQL 的 PIVOT 函数来实现我想要实现的目标?这是我的沙盒:http://sqlfiddle.com/#!18/e198d/1
现在我得到:
topic ThisReply ThatReply AnotherReply
Is the earth flat? (null) (null) (null)
【问题讨论】:
-
我猜您需要返回动态列数?
标签: sql sql-server tsql pivot