行转列,使用case when then
select
date,
sum(case c.type when 'try-pay' then c.sum else 0 end ) as 'try-pay',
sum(case c.type when 'pay-real' then c.sum else 0 end )as 'pay-real',
sum(case c.type when 'pay-success' then c.sum else 0 end )as 'pay-success',
sum(case c.type when 'pay-failure' then c.sum else 0 end )as 'pay-failure',
sum(case c.type when 'notify-input' then c.sum else 0 end )as 'notify-input',
sum(case c.type when 'normal-query' then c.sum else 0 end )as 'normal-query'
from
(SELECT sum(f_count) as sum ,f_type as type ,f_date as date FROM t_log_analyse
group by f_type,f_date order by f_date desc)
as c
group by date
本文介绍了一种使用SQL中的CASE WHEN THEN语句实现行数据到列数据转换的方法。通过具体示例展示了如何根据不同类型的计数数据进行汇总,并将这些计数按类型分别展示为不同的列。
4936

被折叠的 条评论
为什么被折叠?



