SQLServer行列互转实现思路(聚合函数)
作者:我是攻城狮 时间:2024-01-15 15:13:33
有时候会碰到行转列的需求(也就是将列的值作为列名称),通常我都是用 CASE END + 聚合函数来实现的。
如下:
declare @t table
(StudentName nvarchar(20),
Subject nvarchar(20),
Score int)
Insert into @t (StudentName,Subject,Score) values ( '学生A', '中文', 80 );
Insert into @t (StudentName,Subject,Score) values ( '学生A', '数学', 78 );
Insert into @t (StudentName,Subject,Score) values ( '学生A', '英语', 92 );
Insert into @t (StudentName,Subject,Score) values ( '学生B', '中文', 89 );
Insert into @t (StudentName,Subject,Score) values ( '学生B', '数学', 87 );
Insert into @t (StudentName,Subject,Score) values ( '学生B', '英语', 75 );
Insert into @t (StudentName,Subject,Score) values ( '学生C', '中文', 92 );
Insert into @t (StudentName,Subject,Score) values ( '学生C', '数学', 74 );
Insert into @t (StudentName,Subject,Score) values ( '学生C', '英语', 65 );
Insert into @t (StudentName,Subject,Score) values ( '学生D', '中文', 79 );
Insert into @t (StudentName,Subject,Score) values ( '学生D', '数学', 83 );
Insert into @t (StudentName,Subject,Score) values ( '学生D', '英语', 81 );
Insert into @t (StudentName,Subject,Score) values ( '学生E', '中文', 73 );
Insert into @t (StudentName,Subject,Score) values ( '学生E', '数学', 84 );
Insert into @t (StudentName,Subject,Score) values ( '学生E', '英语', 93 );
Insert into @t (StudentName,Subject,Score) values ( '学生F', '中文', 79 );
Insert into @t (StudentName,Subject,Score) values ( '学生F', '数学', 86 );
Insert into @t (StudentName,Subject,Score) values ( '学生F', '英语', 84 );
select StudentName,
sum(case when Subject = N'中文' then Score else 0 end) Chinese,
sum(case when Subject = N'数学' then Score else 0 end) Math,
sum(case when Subject = N'英语' then Score else 0 end) Engilsh
from @t
group by StudentName
今天看到一个新的写法,pivot 可以实现相同的功能(2005才开始支持)。
pivot 的语法为:
table_source
pivot(聚合函数(value_column) pivot_column for (columnlist))
稍微解释一下:
table_source:是我们要进行转换的表。pivot_column: 就是要进行行转列的列名。value_column:是转换后列的值。columnlist 是要生成的列。
同样是上面的例子,使用pivot 可以这样写得到同样的结果:
select StudentName,
[中文] Chinese,
[数学] Math,
[英语] English
from
(select * from @t) t1
pivot (sum(Score) for Subject in([中文],[英语],[数学])) t2
与之对应的 unpivot 就是列转行了(列名作为值),
unpivot 的语法为:
table_source
unpivot(value_column ubpivot_column for(columnlist))
参数的意义与pivot 是一样的。这里我们可以简单的把刚刚转后的再转回去,这样就得到原来的表了:
select StudentName,
Subject,
Score
from
(select * from @t) t1
pivot (sum(Score) for Subject in([中文],[英语],[数学])) t2
unpivot (Score for Subject in([中文],[英语],[数学])) t3
标签:SQLServer,行列互转,聚合函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
smarty简单分页的实现方法
2023-11-21 08:59:48
pip更新问题的解决:'python -m pip install --upgrade pip' 报错问题(最新推荐)
2023-04-23 04:32:13
![](https://img.aspxhome.com/file/2023/8/105028_0s.png)
Bootstrap进度条学习使用
2024-04-18 09:28:31
![](https://img.aspxhome.com/file/2023/4/136614_0s.jpg)
pytorch中index_select()的用法详解
2022-01-20 19:44:05
详细解读Python中的json操作
2022-02-21 00:55:13
![](https://img.aspxhome.com/file/2023/3/81593_0s.png)
eval(function(p,a,c,k,e,d)系列解密javascript程序
2024-04-19 10:01:20
python pickle 和 shelve模块的用法
2023-11-07 22:41:11
JS实现仿百度文库评分功能
2024-05-11 09:34:05
![](https://img.aspxhome.com/file/2023/3/126163_0s.gif)
从xml中获取城市,省份名称
2008-09-05 15:07:00
科讯cms文章分页列表页码样式的修改
2008-07-10 13:11:00
![](https://img.aspxhome.com/file/UploadPic/20087/10/kesion-cms_17s.jpg)
sql中返回参数的值
2024-01-29 11:29:00
基于Vue3实现印章徽章组件的示例代码
2023-07-02 16:54:28
![](https://img.aspxhome.com/file/2023/4/139814_0s.png)
微软开源最强Python自动化神器Playwright(不用写一行代码)
2024-01-02 00:38:00
![](https://img.aspxhome.com/file/2023/9/95419_0s.gif)
解析Anaconda创建python虚拟环境的问题
2023-08-01 03:58:13
![](https://img.aspxhome.com/file/2023/6/61196_0s.png)
Python 使用PIL numpy 实现拼接图片的示例
2021-03-20 02:50:20
JavaScript里实用的原生API汇总
2024-04-30 08:54:06
python tkinter库实现气泡屏保和锁屏
2021-09-17 22:37:57
![](https://img.aspxhome.com/file/2023/2/93602_0s.jpg)
python 8种必备的gui库
2021-10-28 21:10:01
SQL Server性能的改进得益于逻辑数据库设计
2009-10-23 13:55:00
ASP无组件汉字验证码
2008-05-08 13:19:00
![](https://img.aspxhome.com/file/UploadPic/up/2008050814170453.gif)