SQL Server纵表与横表相互转换的方法
时间:2020-10-31 14:31 作者:admin610456
1,纵表转横表
纵表结构 Table_A:
转换后的结构:
纵表转横表的SQL示例:
SELECT Name ,SUM(CASE WHEN Course = N'语文' THEN GradeELSE 0END) AS Chinese ,SUM(CASE WHEN Course = N'数学' THEN GradeELSE 0END) AS Mathematics ,SUM(CASE WHEN Course = N'英语' THEN GradeELSE 0END) AS EnglishFROM dbo.Table_AGROUP BY Name
2,横表转纵表
横表结构Table_B:
转换后的表结构:
横表转纵表的SQL示例:
SELECT Name ,'Chinese' AS Course ,Chinese AS ScoreFROM dbo.Table_BUNION ALLSELECT Name ,'Mathematics' AS Course ,Mathematics AS ScoreFROM dbo.Table_BUNION ALLSELECT Name ,'English' AS Course ,English AS ScoreFROM dbo.Table_BORDER BY Name ,Course DESC
以上所述是小编给大家介绍的SQL Server纵表与横表相互转换的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
(责任编辑:admin)