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

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

相关推荐
IT_陈寒1 小时前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
用户395240998802 小时前
排坑日记:ASP.NET Core 中 "Required field is not provided" 验证错误全记录
后端
用户8356290780513 小时前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
Oneslide4 小时前
sudo免密权限配置不生效
后端
站大爷IP4 小时前
为什么Python不用var或let声明变量?
后端
赴星半途4 小时前
NestJS实战-创建AuthService
后端
北冥有鱼4 小时前
mqtt 测试
前端·后端
代码丰4 小时前
使用 TtlExecutors 解决线程池中的 ThreadLocal 上下文丢失问题
后端
阿祖zu4 小时前
别再优化 RAG 了,适配 Agent 的 LLM Wiki 知识库理念
前端·后端·aigc