`

纵行数据以横行查询

SQL 
阅读更多
纵行数据以查询横行

以学生成绩表为例:




select name,
       decode(yw, null, '-', yw) 语文,
       decode(sx, null, '-', sx) 数学,
       decode(yy, null, '-', yy) 英语
  from (select name, sum(yw) yw, sum(sx) sx, sum(yy) yy
          from (select name,
                       case cname
                         when '语文' then
                          score
                       end as yw,
                       case cname
                         when '数学' then
                          score
                       end as sx,
                       case cname
                         when '英语' then
                          score
                       end as yy
                  from student)
         group by name)


查询结果:



  • 大小: 14.6 KB
  • 大小: 8.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics