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

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

相关推荐
小王C语言3 分钟前
Windows和Linux之间文件互传
linux·运维·服务器
Dxy12393102168 分钟前
Python请求方式介绍:JSON、表单及其他常见数据传输格式
数据库·python·json
迷渡14 分钟前
聊一聊 Bun 用 Rust 重写这件事
开发语言·后端·rust
王中阳Go17 分钟前
秒杀、分库分表、全链路追踪:一个电商微服务的架构全拆解
后端·go
正儿八经的少年24 分钟前
Spring Boot 两种激活配置方式的作用与区别
java·spring boot·后端
回家路上绕了弯1 小时前
AgentScope Java实战博客:从入门到落地,解锁智能代理开发新范式
后端
疯狂成瘾者1 小时前
Spring Boot 项目中的 SMTP 邮件验证码服务技术解析
java·spring boot·后端
阿苟1 小时前
消息队列重点详解
后端·面试
magic_now1 小时前
Linux 内核启动流程详解(基于 5.15.119 源码)
linux·运维·服务器
RustCoder1 小时前
MangoFetch:一个用 Rust 写的 CLI/TUI 高性能的下载工具
后端·rust·开源