t-sql中pivot用法(行列转换)
从另一张表找的3974行是张三,8319行是李四,3051行是王五;
从stu_score查id,stu_id两列,聚合求出 得出张三,李四,王五在stu_score中的记录条数
from
(select id,stu_id from stu_score) as s
pivot
(
count(id)
for stu_id in ([3974],[8319],[3051])
)as pvt
T-SQL Pivot Syntax
SELECT
[non-pivoted column], -- optional
[additional non-pivoted columns], -- optional
[first pivoted column],
[additional pivoted columns]
FROM (
SELECT query producing sql da
-- select pivot columns as dimensions and
-- value columns as measures from sql tables
) AS TableAlias
PIVOT
(
<aggregation function>(column for aggregation or measure column) -- MIN,MAX,SUM,etc
FOR [<column name containing values for pivot table columns>]
IN (
[first pivoted column], ..., [last pivoted column]
)
) AS PivotTableAlias
from
(select dept_id, exam_name, [language]
from stu_score,stu_studentinfo
where stu_score.stu_id = stu_studentinfo.id) as t
pivot
(
avg([language])
for dept_id in ([407],[408],[409],[415])
)as pvt
结果如下:
考试名称 | 一班 | 二班 | 三班 | 九班 |
考试一 | 89.26 | 88.33 | 90.36 | 85.25 |
考试二 | 82.26 | 87.98 | 80.36 | 85.25 |
期末 | 81.26 | 83.33 | 80.36 | 78.25 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?