SQL中UNPIVOT是什么

您所在的位置:网站首页 转行是什么意思 SQL中UNPIVOT是什么

SQL中UNPIVOT是什么

2024-07-14 04:40| 来源: 网络整理| 查看: 265

比如你有一个table,你想把列转换成行,就可以使用unpivot。还是有点抽象,具体什么意思呢?

比如说有一个table stu_score如下:

Name, Math, Chinese zhc, 99, 95 xiaoy, 89, 100 xiaot, 98, 60

你想要把它转化成这样:

Name, Subject, Score zhc, Math, 99 zhc, Chinese, 95 xiaoy, Math, 89 xiaoy, Chinese, 100 xiaot, Math, 98 xiaot, Chinese, 60

就是把一行转换成多行,可以这样写SQL用unpivot:

select u.name, u.subject, u.score from stu_score unpivot ( score for subject in (Math, Chinese) ) u;

另外一种方法还可以使用union来进行一行到多行的转换,相对来说比unpivot容易理解。 好了,你懂了吗?

 

原文:http://blog.csdn.net/hongchangfirst/article/details/111306968

作者:hongchangfirst

hongchangfirst的主页:http://blog.csdn.net/hongchangfirst

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3