脚本:
select stat_date,class_id,
case when origin ='ad' then sum(pv) else 0 end as m1,
case when origin<>'ad' then sum(pv) else 0 end as m2
from t_stat_goods_page_by_hour
where
datediff(stat_date,'2008-12-17')>=0 and datediff(stat_date,'2008-12-23')<=0
and page_type='m1'
group by stat_date,origin,class_id
注意的是 case when 中的区分词 (如例子脚本中的 origin字段必须在 group by 中,否则会出现某列为0的情况)
本文介绍了一种使用SQL进行数据筛选与汇总的方法,通过CASE WHEN语句来区分不同的数据来源,并进行了有效的分组,确保了统计数据的准确性。特别关注了CASE WHEN条件中的字段必须包含在GROUP BY子句中这一细节。
2063

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



