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

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

相关推荐
captain3761 天前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
闪亮的路灯1 天前
Linux 安装mqtt服务mosquitto
linux·运维·服务器
小此方1 天前
Re:Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
xszivvvv1 天前
Mariadb数据库frm文件异常,如何恢复数据
数据库·mariadb
深蓝AI1 天前
Python 3.13 的 Queue.shutdown:异步消费者终于能正常收工了
后端
smile_life_1 天前
MySQL 设置 防止账户被暴力破解
数据库·mysql
ShineWinsu1 天前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup
武子康1 天前
MultiNet6 空闲崩溃:`EXCVADDR=0x8` 后又出现 `CORRUPT HEAP`
人工智能·后端·agent
测试盐1 天前
oracle数据库卡顿排查
数据库·oracle
滕州市燕猫虎计算机科技工作室个体工商户1 天前
MySQL、MVCC、多版本并发控制
数据库·mysql·mvcc·数据库事务