宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 解决办法
  3. 正文

SQL列转行、行转列

2018年1月6日 471点热度 0人点赞 0条评论

— 列传行

select name 姓名,
   max(case subject when '语文' then score end) 语文,
   max(case subject when '数学' then score end) 数学,
   max(case subject when '物理' then score end) 物理
from studentA
group by name

— 行转列 

select * from (
   select 姓名, '语文' as 科目, 语文 as 分数 from studentB
   union all
   select 姓名, '数学' as 科目, 数学 as 分数 from studentB
   union all
   select 姓名, '物理' as 科目, 物理 as 分数 from studentB
   ) a

博主理解:把语文列装换为分数列,本身列头没有了,使用字符来填充列头,代表本身列具备的含义。

单个示例:select  ‘语文’ as 科目, 语文 as 分数 from studentB

原文链接:https://www.jianshu.com/p/ecb02658239a

标签: 暂无
最后更新:2022年3月23日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2