SQL将一行转换为两列(标签值)

时间:2019-01-29 10:21:26

标签: sql sql-server pivot unpivot

我查询数据库并获得以下行:

**NAME | DESCRIPTION | MOBILE | EMAIL | CENTER |**

George | Nice guy    | 644213568 | geo@gmail.com | NASA |

我想将其转换为:

LABEL | VALUE

Name | George

DESCRIPTION | Nice guy

MOBILE | 644213568

EMAIL | geo@gmail.com

CENTER | NASA

提前谢谢!!!!

1 个答案:

答案 0 :(得分:1)

您想要apply

select tt.LABEL, tt.VALUE
from table t cross apply
     ( values ('DESCRIPTION', DESCRIPTION), ('MOBILE', MOBILE), ('EMAIL', EMAIL), ('CENTER', CENTER) 
     ) tt(LABEL, VALUE)
 where t.USER_ID = 23;