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

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

相关推荐
Anokata10 小时前
MYSQL SQL 执行系列1
android·sql·mysql
JSON_L10 小时前
MongoDB文档关系详解
数据库·mongodb
玉宇夕落10 小时前
Node.js 路径模块(path)的学习
后端
glartchen10 小时前
旧电脑配置Pixi 环境下 Whisper-CTranslate2 的若干坑与修复指南
后端
济*沧*海10 小时前
创建springboot项目时会出现注释乱码情况
java·spring boot·后端
IT_陈寒10 小时前
Vite冷启动快?我遇到了个奇怪的依赖问题
前端·人工智能·后端
geovindu10 小时前
java: Builder Pattern
java·开发语言·后端·设计模式·建造者模式·生成器模式
郝学胜_神的一滴10 小时前
系统设计 019:分片拆分与副本容灾深度解析
后端·架构
程序员cxuan10 小时前
Cursor 掀桌子了,Grok 4.5 这次要上天了?
人工智能·后端·程序员
卓怡学长10 小时前
w268基于springboot + vue 物流系统
java·数据库·vue.js·spring boot·spring·intellij-idea