首页 > 编程知识 正文

pivot sqlserver,unpivot用法 oracle

时间:2023-05-06 17:04:46 阅读:239317 作者:3956

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

比如说有一个table stu_score如下:

Name, Math, Chinesezhc, 99, 95xiaoy, 89, 100xiaot, 98, 60

你想要把它转化成这样:

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

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

select u.name, u.subject, u.scorefrom stu_scoreunpivot( score for subject in (Math, Chinese)) u;

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

 

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

作者:hongchangfirst

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

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。