如何透视表数据

时间:2016-04-29 07:17:22

标签: sql-server pivot

我的表格结构如下:

Person_id      Add_Type        Address
1              Present         A
1              Permanent       B

我想要数据透视表如下:

Person_id      Present       Permanent
1                A             B

1 个答案:

答案 0 :(得分:1)

首先尝试使用任何搜索引擎......这就是你需要的:

示例数据

create table #t
(
   Person_id VARCHAR(MAX),
   Add_Type VARCHAR(MAX),
   Address_ VARCHAR(MAX)
)
insert into #t values ('1','Present', 'A'), ('1','Permanent', 'B')

<强> QUERY

select *
from (select Person_id, Add_Type, Address_ 
      from #t 
      )as src
pivot
(
   min(Address_)
   for Add_Type in ([Present], [Permanent])
) as pvt

<强>输出

Person_id      Present       Permanent
1              A             B