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

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

相关推荐
llxxyy卢1 天前
JWT安全&预编译CASE注入
数据库·sql·安全
⑩-1 天前
SpringCloud-Sleuth链路追踪实战
后端·spring·spring cloud
冷崖1 天前
原子锁操作
c++·后端
moxiaoran57531 天前
Spring AOP开发的使用场景
java·后端·spring
一线大码1 天前
Gradle 基础篇之基础知识的介绍和使用
后端·gradle
Java猿_1 天前
Spring Boot 集成 Sa-Token 实现登录认证与 RBAC 权限控制(实战)
android·spring boot·后端
wanhengidc1 天前
云手机的适配性怎么样?
运维·服务器·安全·智能手机·云计算
小王师傅661 天前
【轻松入门SpringBoot】actuator健康检查(上)
java·spring boot·后端
jimy11 天前
安卓里运行Linux
linux·运维·服务器
大布布将军1 天前
⚡️ 深入数据之海:SQL 基础与 ORM 的应用
前端·数据库·经验分享·sql·程序人生·面试·改行学it