首页 > 开发 > 综合 > 正文

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

2024-07-21 02:42:02
字体:
来源:转载
供稿:网友
纵表转横表的"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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表