纵表 转 横表 sql

fname       ftype             fvalue
小乔        zaocan              10
小乔       zhongcan             20
小乔        wancan               5


转换后的表结构:
fname       zaocan_value          zhongcan_value       wancan_value
小乔                  10                                 20                             5

纵表转横表SQL示例:
select Fname,
       sum(case Ftype
             when 'zaocan' then
              Fvalue
             else
              0
           end) as zaocan_value,
       sum(case Ftype
             when 'zhongcan' then
              Fvalue
             else
              0
           end) as zhongcan_value,
       sum(case Ftype
             when 'wancan' then
              Fvalue
             else
              0
           end) as wancan_value
  from case_table
 group by Fname

资料引用:http://www.knowsky.com/395671.html

你可能感兴趣的:(html,sql)