SQL行列转置
时间:2021-07-01 10:21:17
帮助过:15人阅读
函数
alter function zh()
returns table
as
return(
select 科目,
max(张三)
as 张三,
max(李四)
as 李四
from(
select ‘语文‘as 科目,张三,李四
from stu2
pivot(max(语文)
for 姓名
in (科目,张三,李四))
as a
union all
select ‘数学‘as 科目,张三,李四
from stu2
pivot(max(数学)
for 姓名
in (科目,张三,李四))
as a )
as n
group by 科目
go
select * from zh()
--存储过程
alter procedure zh1
as
select 科目,
max(张三)
as 张三,
max(李四)
as 李四
from(
select ‘语文‘as 科目,张三,李四
from stu2
pivot(max(语文)
for 姓名
in (科目,张三,李四))
as a
union all
select ‘数学‘as 科目,张三,李四
from stu2
pivot(max(数学)
for 姓名
in (科目,张三,李四))
as a )
as n
group by 科目
go
exec zh1
SQL行列转置
标签:存储过程 sele alter returns nio sql col select 分享图片