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

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

相关推荐
果果软件9 小时前
2026 后台管理系统选型:若依、JeecgBoot、芋道、NocoBase 和 GewuMind Boot 怎么选?
java·人工智能·后端·ai·开源
djjjx.9 小时前
Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
时间的拾荒人10 小时前
Linux 系统命令与网络、磁盘、日志监控(下篇):性能原理与优化
linux·运维·网络
Sashtok13 小时前
cpp基础知识(10)
c++·后端
CoderYanger13 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
行百里er13 小时前
【Spring Insight 技术分享】runtime-settings.json 的妙用
后端·开源·监控
MSTcheng.14 小时前
【MySQL】MySQL数据库——表的操作
数据库·mysql·adb
databook15 小时前
处理一批 PDF,怎么算真的处理完了
后端·python
Yeats_Liao15 小时前
物联网平台MongoDB实战|(一)为什么选择MongoDB
数据库·物联网·mongodb
ZhengEnCi15 小时前
覆盖索引是什么:用借书和外卖看懂回表这件事
sql·mysql·postgresql