【问题标题】:SQL Server : group by userId - merge 2 rows into 1 rowSQL Server:按用户 ID 分组 - 将 2 行合并为 1 行
【发布时间】:2020-02-18 23:10:17
【问题描述】:

当我通过UserName 查询用户时,我得到两行,但我想将它们合并为一行。例如,

当前结果:

想要的结果:

【问题讨论】:

标签: sql sql-server pivot


【解决方案1】:

你可以做聚合:

select userid, username, 
       max(case when phonetype = 'primary' then phonenumber end) as primary_no,
       max(case when phonetype = 'secondary' then phonenumber end) as secondary_no
from t
group by userid, username;

【讨论】:

    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2012-06-26
    • 1970-01-01
    • 2012-06-20
    相关资源
    最近更新 更多