sql 行列转换
create table #test1(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int)
insert into #test1 values(1,'a',1000,2000,4000,5000)
--insert into #test values(2,'b',3000,3500,4200,5500)
select * from #test1
select profile
from
#test1
unpivot
(
profile
for quarter in
([Q1],[Q2],[Q3],[Q4])
)
as unpvt
表一 学生成绩表
编号 课程 成绩
001 计算机 65
002 计算机 75
003 体育 54
004 美术 65
005 体育 54
001 体育 56
现 想让查询结果达到如下 要求
学生编号 计算机成绩 体育成绩 美术成
001 65 56 75
.......
--典型的列变行,用动态语句来做
--测试数据
declare @tab table(id varchar(3),class varchar(10),sums int)
insert @tab select '001','计算机','65' union all
select '002','计算机','75' union all select '001','美术','77' union all
select '003','体育','54' union all select '004','美术','65' union all
select '005','体育','55' union all select '001','体育','56' union all
select '002','体育','88' union all select '002','美术','98' union all
select '003','计算机','82' union all select '003','美术','92' union all
select '004','计算机','85' union all select '004','体育','25' union all
select '005','计算机','89' union all select '005','美术','99';
--select * from @tab
--下面是测试代码
select * into #temp1 from @tab declare @str varchar(8000)
set @str='select id as 编号,' select @str=@str+'
sum(case class when'''+class+'''then sums else 0 end) as'''+class+'成绩'+''','
from (select distinct class from #temp1)t
set @str=left(@str,len(@str)-1)+' from #temp1 group by id' exec(@str)
drop table #temp1
/* 下面是测试结果
(所影响的行数为 15 行)
(所影响的行数为 15 行)
编号 计算机成绩 美术成绩 体育成绩
---- ----------- ----------- -----------
--测试数据
declare @tab table(id varchar(3),class varchar(10),sums int)
insert @tab select '001','计算机','65' union all
select '002','计算机','75' union all select '001','美术','77' union all
select '003','体育','54' union all select '004','美术','65' union all
select '005','体育','55' union all select '001','体育','56' union all
select '002','体育','88' union all select '002','美术','98' union all
select '003','计算机','82' union all select '003','美术','92' union all
select '004','计算机','85' union all select '004','体育','25' union all
select '005','计算机','89' union all select '005','美术','99';
--select * from @tab
--下面是测试代码
select * into #temp1 from @tab declare @str varchar(8000)
set @str='select id as 编号,' select @str=@str+'
sum(case class when'''+class+'''then sums else 0 end) as'''+class+'成绩'+''','
from (select distinct class from #temp1)t
set @str=left(@str,len(@str)-1)+' from #temp1 group by id' exec(@str)
drop table #temp1
/* 下面是测试结果
(所影响的行数为 15 行)
(所影响的行数为 15 行)
编号 计算机成绩 美术成绩 体育成绩
---- ----------- ----------- -----------
001 65 77 56
002 75 98 88
003 82 92 54
003 82 92 54
004 85 65 25
005 89 99 55 */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?