group by+ sum case 同一字段 不同值的分组统计

需求描述:对文章按国家分组,统计正面,负面,中立态度分别统计

select 
(SELECT  cn_name  FROM country_info where auto_id= t.country_id) name,
t.country_id,
sum(case when t.support_level=1 then 1 else 0 end) zheng,
sum(case when t.support_level=2 then 1 else 0 end) fu,
sum(case when t.support_level=3 then 1 else 0 end) zhongli
 from trace_info_ref t where t.conf_id in (123,345) group by t.country_id 

#分析

1.此句sql country_id和 country name的关联写法值得学习。

2.此句的 sum case写法 统计同一字段不同值的写法值得学习。

需求:按用户分组统计转发的文章和参与的话题

分析:转发包含字符串 //@ ,参与话题 包含 #话题# 

select  count(1) total_num,
 sum(case when `fulltext` like '//@%' then 1 else 0 end) tag1_num,
sum(case when `fulltext` like '%#%#%' then 1 else 0 end) tag2_num ,      
usercode 
from sj_test group by usercode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值