1.原始数据
2.将数据中value1和value2根据co_id显示到一行:
示例代码
--1.创建表
create table #ch
(co_id int,sn varchar(5),value1 varchar(50),value2 varchar(50))
--2.初始化数据
insert into #ch
select 1,'1','2015-05-01 00:00:00.000','10.00%' union all
select 1,'2','2016-05-01 00:00:00.000','15.00%'
--3.原始数据展示
select * from #ch
--4.多行转列的结果
select co_id,[1(value1)],[1(value2)],[2(value1)],[2(value2)]
from
(select co_id,sn+'('+c+')' 'c',v
from
#ch a
unpivot(v for c in([value1],[value2])) u) b
pivot(max(v) for c in([1(value1)],[1(value2)],[2(value1)],[2(value2)])) p
--5.清除临时表
drop table #ch
参考 https://blog.csdn.net/xnlzg/article/details/55668531