PIVOT two columns and keep others as they are

假装没事ソ 提交于 2019-12-06 08:11:05

Using conditional aggregation:

select 
    Fname = max(case when name = 'Fname' then value end)
  , Lname = max(case when name = 'Lname' then value end)
  , RefId
from t
group by RefId

rextester demo: http://rextester.com/MRMY11592

returns:

+---------+---------+-------+
|  Fname  |  Lname  | RefId |
+---------+---------+-------+
| John    | Smith   | 32145 |
| Peter   | Mahlang | 34589 |
+---------+---------+-------+

Or using pivot()

select 
    Fname
  , Lname
  , RefId
from (select name, value, refid from t) s
pivot(max(value) for name in ([Fname],[Lname]))p
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!