mysql 多行变一行

来源:互联网 发布:php变量的作用域 编辑:程序博客网 时间:2024/04/29 23:16

 DateClass       Class         Count   
  2004-02             AU                 32   
  2004-02             VAIO             56   
  2004-02             PB                 77   
  2004-02             TV                 89   
  2004-03             AU                 38   
  2004-03             VAIO             99   
  2004-03             PB                 32   
  2004-03             TV                 87   
    
    
  要将查询结果变为   
    
  DateClass         AU       VAIO         PB       TV   
  2004-02             32         56           77       89   
  2004-03             38         99           32       87  


SQL实现:

select   DataClass   ,     
                max(case   when   class   =   'AU'   then   Count   else   0   end)   as   AU,   
                max(case   when   class   =   'VAIO'   then   Count   else   0   end)   as   VAIO,   
                max(case   when   class   =   'PB'   then   Count   else   0   end)   as   PB,   
                max(case   when   class   =   'TV'   then   Count   else   0   end)   as   TV   
  from   test   
  group   by   DataClass;


0 0
原创粉丝点击