我有像
这样的场景User | Session | Response
u1 | s1 | r1
u1 | s1 | r2
u1 | s2 | r3
u2 | s2 | r4
u2 | s2 | r5
AND输出需要像
OUT PUT表
User | Session | response1 | response2 | Session.....n
u1 | s1 | r1 | r2
u1 | s2 | r3 | NULL
u2 | s2 | r4 | r5
该会话列可能是n次,具体取决于号码响应。
答案 0 :(得分:4)
您可以实现PIVOT
功能来执行此数据转换。如果您有已知数量的值,则可以对查询进行硬编码:
select *
from
(
select [user], session, response,
'response'+ cast(row_number() over(partition by [user], session order by response) as varchar(10)) rn
from yourtable
) src
pivot
(
max(response)
for rn in ([response1], [response2])
) piv
但是如果您的响应数量未知,那么您将需要使用动态sql:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME('response'+ cast(row_number() over(partition by [user], session order by response) as varchar(10)))
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT [user], session, ' + @cols + ' from
(
select [user], session, response,
''response''+ cast(row_number() over(partition by [user], session order by response) as varchar(10)) rn
from yourtable
) x
pivot
(
max(response)
for rn in (' + @cols + ')
) p '
execute(@query)
两者都会产生相同的结果:
| USER | SESSION | RESPONSE1 | RESPONSE2 |
------------------------------------------
| u1 | s1 | r1 | r2 |
| u1 | s2 | r3 | (null) |
| u2 | s2 | r4 | r5 |