电脑技术学习

教你轻松掌握一个纵表转横表的"SQL"

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