[典型的行列转换,case when可以搞定。

来源:百度文库 编辑:神马文学网 时间:2024/04/28 18:52:58
表结构:
        name               type                  value
               lee                breakfast                10
               lee               lunch                        20
              lee                supper                     20
怎么用一句sql查询出如下结果:
        name            breakfast        lunch          supper
               lee                 10                20                  20  select name,
          sum(case  type when 'breakfast' then value end) breakfast,
          sum(case  type when 'lunch' then value end) lunch,
          sum(case  type when 'supper' then value end) supper,
  from your_tbl
group by name