简体   繁体   中英

SQL Server : group by userId - merge 2 rows into 1 row

When I query a user by UserName I get two rows but I would like to merge them into one row. For example,

Current result:

在此处输入图片说明

Desired result:

进入

You can do aggregation :

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM