原始的数据结构是这样的:
这是一个信息表,记录下每个app每天对应什么等级
现在需求是:统计每天每个等级有多少个app?
实现的sql如下:
select count(*) as num,level,date from t_app_level_status group by date,level
结果如下:
这样有个问题,不方便在程序下显示,如果需要把这里的行结构变成列结构,使用下面的sql:
select date, MAX (CASE level WHEN 1 THEN num ELSE 0 END ) as "1", MAX (CASE level WHEN 2 THEN num ELSE 0 END ) as "2", MAX (CASE level WHEN 3 THEN num ELSE 0 END ) as "3" from (select count(*) as num, level, date from t_app_level_status group by date, level) temp group by date;