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

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

相关推荐
qq_192779875 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
u0109272715 小时前
使用Plotly创建交互式图表
jvm·数据库·python
爱学习的阿磊5 小时前
Python GUI开发:Tkinter入门教程
jvm·数据库·python
小小管写大大码6 小时前
如何让vscode变得更智能?vscode接入claude实现自动编程
运维·ide·vscode·自动化·编辑器·ai编程·腾讯云ai代码助手
tudficdew6 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python
zhang133830890756 小时前
CG-09H 超声波风速风向传感器 加热型 ABS材质 重量轻 没有机械部件
大数据·运维·网络·人工智能·自动化
sjjhd6527 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python
Configure-Handler7 小时前
buildroot System configuration
java·服务器·数据库
2301_821369617 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.8 小时前
Keepalived VIP迁移邮件告警配置指南
运维·服务器·笔记