示例数据:
输出应为:
我想问一下MSSQL中我可以运行什么脚本来获得那种输出。希望有人可以帮助我,我仍然是SQL编程的新手。非常感谢你。
答案 0 :(得分:1)
使用带有row_number()的CTE:
with CTE as
(
select T1.*, row_number() over(partition by USERNAME order by TRANSACTION_TIME) X_ORD
from Table1 T1
)
select distinct
A1.USERNAME,
A1.REGISTRATION_DATE,
A2.Transaction_Time as First_X,
A2.RELOAD_AMOUNT as First_R,
A2.CHANNEL as First_C,
A3.Transaction_Time as Second_X,
A3.RELOAD_AMOUNT as Second_R,
A3.CHANNEL as Second_C
from CTE A1
left join CTE A2
on A1.USERNAME = A2.USERNAME
and A2.X_ORD = 1
left join CTE A3
on A1.USERNAME = A3.USERNAME
and A3.X_ORD = 2