纵表转横表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

你可能感兴趣的:(sql)