SQLServer列转行操作及union all用法

1.创建测试表及数据sql如下

create table ScoresTable(

Name varchar(50),

ChineseScore int,

MathScore int

)

insert into ScoresTable values('小张',90,95)

insert into ScoresTable values('小王',98,99)

2.表中查询结果如下

3.现需列转行显示,每行显示 姓名,科目,成绩

实现sql如下:

select * from

(

select Name, Subject ='语文',Scores=ChineseScore from ScoresTable

union all

select Name, Subject ='数学',Scores=MathScore from ScoresTable

) t

order by Name ,Subject

运行结果如下,实现了列转行

相关推荐
ulias2121 天前
Linux系统中的权限问题
linux·运维·服务器
码事漫谈1 天前
当AI开始“思考”:我们是否真的准备好了?
前端·后端
沃尔威武1 天前
数据库 Sinks(.net8)
数据库·.net·webview
青花瓷1 天前
Ubuntu下OpenClaw的安装(豆包火山API版)
运维·服务器·ubuntu
问简1 天前
docker 镜像相关
运维·docker·容器
Dreamboat¿1 天前
SQL 注入漏洞
数据库·sql
Dream of maid1 天前
Linux(下)
linux·运维·服务器
齐鲁大虾1 天前
统信系统UOS常用命令集
linux·运维·服务器
铁东博客1 天前
Go实现周易大衍筮法三变取爻
开发语言·后端·golang
Benszen1 天前
Docker容器化技术实战指南
运维·docker·容器