create table MobileInfo
(
Id int not null primary key identity(1,1),
中国联通 int not null,
中国移动 int not null,
中国电信 int not null,
港澳通 int not null
)insert into MobileInfo select 5 , 8 , 6 , 4 union
select 5 , 6 , 9 ,1 union
select 5 , 7 , 3 , 0 select * from MobileInfo 原表数据:
编号 中国联通 中国移动 中国电信 港澳通
1 5 8 6 4
2 5 6 9 1
3 5 7 3 0
要实现的结果:
编号 营业商 对应号
1 中国联通 5
1 中国移动 8
1 中国电信 6
1 港澳通 4
2 中国联通 5
2 中国移动 6
2 中国电信 9
2 港澳通 1
3 中国联通 5
3 中国移动 7
3 中国电信 3
3 港澳通 0
在线求解答。
(
Id int not null primary key identity(1,1),
中国联通 int not null,
中国移动 int not null,
中国电信 int not null,
港澳通 int not null
)insert into MobileInfo select 5 , 8 , 6 , 4 union
select 5 , 6 , 9 ,1 union
select 5 , 7 , 3 , 0 select * from MobileInfo 原表数据:
编号 中国联通 中国移动 中国电信 港澳通
1 5 8 6 4
2 5 6 9 1
3 5 7 3 0
要实现的结果:
编号 营业商 对应号
1 中国联通 5
1 中国移动 8
1 中国电信 6
1 港澳通 4
2 中国联通 5
2 中国移动 6
2 中国电信 9
2 港澳通 1
3 中国联通 5
3 中国移动 7
3 中国电信 3
3 港澳通 0
在线求解答。
SELECT ID,'中国移动' ,[中国移动] FROM MOBILEINFO UNION ALL
SELECT ID,'中国电信' ,[中国电信] FROM MOBILEINFO UNION ALL
SELECT ID,'港澳通' ,[港澳通] FROM MOBILEINFO
(
Id int not null primary key identity(1,1),
中国联通 int not null,
中国移动 int not null,
中国电信 int not null,
港澳通 int not null
)insert into MobileInfo select 5 , 8 , 6 , 4 union
select 5 , 6 , 9 ,1 union
select 5 , 7 , 3 , 0select id , 营业商 = '中国联通' , 对应号 = 中国联通 from MobileInfo
union all
select id , 营业商 = '中国移动' , 对应号 = 中国移动 from MobileInfo
union all
select id , 营业商 = '中国电信' , 对应号 = 中国电信 from MobileInfo
union all
select id , 营业商 = '港澳通' , 对应号 = 港澳通 from MobileInfo
order by id , 营业商
drop table MobileInfo/*
id 营业商 对应号
----------- -------- -----------
1 港澳通 1
1 中国电信 9
1 中国联通 5
1 中国移动 6
2 港澳通 0
2 中国电信 3
2 中国联通 5
2 中国移动 7
3 港澳通 4
3 中国电信 6
3 中国联通 5
3 中国移动 8(所影响的行数为 12 行)
*/
(
Id int not null primary key identity(1,1),
中国联通 int not null,
中国移动 int not null,
中国电信 int not null,
港澳通 int not null
) insert into MobileInfo select 5 , 8 , 6 , 4 union
select 5 , 6 , 9 ,1 union
select 5 , 7 , 3 , 0 --select * from MobileInfo select id as 编号,营业商,对应号 from MobileInfo unpivot (对应号 for 营业商 in([中国联通] , [中国移动] , [中国电信],[港澳通])) t
drop table MobileInfo
/*编号 营业商 对应号
----------- -------------------------------------------------------------------------------------------------------------------------------- -----------
1 中国联通 5
1 中国移动 6
1 中国电信 9
1 港澳通 1
2 中国联通 5
2 中国移动 7
2 中国电信 3
2 港澳通 0
3 中国联通 5
3 中国移动 8
3 中国电信 6
3 港澳通 4(12 行受影响)*/
GO
create table MobileInfo
(
Id int not null primary key identity(1,1),
中国联通 int not null,
中国移动 int not null,
中国电信 int not null,
港澳通 int not null
)insert into MobileInfo select 5 , 8 , 6 , 4 union
select 5 , 6 , 9 ,1 union
select 5 , 7 , 3 , 0
SELECT * FROM (
SELECT ID '编号','中国联通' '营业商',[中国联通] '对应号' FROM MOBILEINFO UNION ALL
SELECT ID,'中国移动' ,[中国移动] FROM MOBILEINFO UNION ALL
SELECT ID,'中国电信' ,[中国电信] FROM MOBILEINFO UNION ALL
SELECT ID,'港澳通' ,[港澳通] FROM MOBILEINFO
) T ORDER BY [编号] ASC
/*(3 行受影响)
编号 营业商 对应号
----------- -------- -----------
1 港澳通 1
1 中国电信 9
1 中国联通 5
1 中国移动 6
2 港澳通 0
2 中国电信 3
2 中国联通 5
2 中国移动 7
3 港澳通 4
3 中国电信 6
3 中国联通 5
3 中国移动 8(12 行受影响)
*/
(
Id int not null primary key identity(1,1),
中国联通 int not null,
中国移动 int not null,
中国电信 int not null,
港澳通 int not null
) insert into MobileInfo select 5 , 8 , 6 , 4 union
select 5 , 6 , 9 ,1 union
select 5 , 7 , 3 , 0 --select * from MobileInfo --select id as 编号,营业商,对应号 from MobileInfo unpivot (对应号 for 营业商 in([中国联通] , [中国移动] , [中国电信],[港澳通])) tdeclare @sql varchar(8000)
select @sql = isnull(@sql + ' union all ' , '' ) + ' select id as 编号 , [营业商] = ' + quotename(Name , '''') + ' , [对应号] = ' + quotename(Name) + ' from MobileInfo'
from syscolumns
where name! = N'id' and ID = object_id('MobileInfo')
order by colid asc
exec(@sql + ' order by id,营业商 ')drop table MobileInfo
/*3 行受影响)
编号 营业商 对应号
----------- -------- -----------
1 港澳通 1
1 中国电信 9
1 中国联通 5
1 中国移动 6
2 港澳通 0
2 中国电信 3
2 中国联通 5
2 中国移动 7
3 港澳通 4
3 中国电信 6
3 中国联通 5
3 中国移动 8(12 行受影响)*/
from MobileInfo unpivot (对应号 for 营业商 in([中国联通] , [中国移动] , [中国电信],[港澳通])) tPIVOT 和 UNPIVOT 关系运算符对表值表达式进行操作以获得另一个表。PIVOT 通过将表达式某一列中的唯一值转换为输出中的多个列来转换表值表达式,并在必要时对最终输出中所需的任何其余的列值执行聚合。UNPIVOT 与 PIVOT 执行相反的操作,将表值表达式的列转换为列值。
(
id int not null,
中国联通 int not null,
中国移动 int not null,
中国电信 int not null,
中国网通 int not null
)drop table a
insert into a
select 1 ,4,5,6,8 union all
select 2 ,4,5,1,9 union all
select 3 ,1,3,2,0 select * from a
select
id,营业商,代码号
from
(select id,中国联通,中国移动,中国电信,中国网通 from a) as ChinaMobile
unpivot( 代码号 for 营业商 in (中国联通,中国移动,中国电信,中国网通)) as Mobile