关于统计报表常用sql

1 根据条件查总数

select
count(*) all_sum, if(sum(if(problem_state in (1,2),1,0)) is null,0,sum(if(problem_state in (1,2),1,0))) problem_num,
if(sum(if(problem_state=3,1,0)) is null,0,sum(if(problem_state=3,1,0))) handled_num
from problem_feedback where
problem_state in (1,2,3) and
DATE_FORMAT(problem_find_time,'%Y-%m-%d') = #{time} and
department_id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
  #{item}
</foreach>
2根据时间计算总数(用在报表柱上)

select hours,sum(count) count ,sum(rank) rank from (
SELECT
CASE WHEN HOUR(problem_find_time)in(0,1,2) or null THEN '0-2点'
WHEN HOUR(problem_find_time)in(3,4,5) or null THEN '3-5点'
WHEN HOUR(problem_find_time)in(6,7,8) or null THEN '6-8点'
WHEN HOUR(problem_find_time)in(9,10,11) or null THEN '9-11点'
WHEN HOUR(problem_find_time)in(12,13,14) or null THEN '12-14点'
WHEN HOUR(problem_find_time)in(15,16,17) or null THEN '15-17点'
WHEN HOUR(problem_find_time)in(18,19,20) or null THEN '18-20点'
WHEN HOUR(problem_find_time)in(21,22,23) or null THEN '21-23点'
END AS `hours` ,count(HOUR(problem_find_time)) count,0 as rank
from problem_feedback WHERE
problem_state in
<foreach collection="state" index="index" item="item" open="(" separator="," close=")">
  #{item}
</foreach>
and
DATE_FORMAT(problem_find_time,'%Y-%m-%d') = #{time} and
department_id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
  #{item}
</foreach>
group by hours
union select '0-2点'hours , 0 count,1 rank
union select '3-5点' hours , 0 count,2 rank
union select '6-8点' hours, 0 count,3 rank
union select '9-11点' hours, 0 count,4 rank
union select '12-14点' hours, 0 count,5 rank
union select '15-17点' hours, 0 count,6 rank
union select '18-20点' hours, 0 count,7 rank
union select '21-23点' hours, 0 count,8 rank
)aa
group by hours
order by rank
3类似的还有周报表

select week,sum(num) num from (
    select week ,sum(num) num from(
    select weekday(problem_find_time) week,count(problem_find_time) num
    from problem_feedback where problem_find_time is not null
    and
    problem_state in
    <foreach collection="state" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    and
    DATE_FORMAT(problem_find_time,'%Y-%m-%d') BETWEEN #{beginTime} and #{endTime} and
    department_id in
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    group by  weekday(problem_handle_time)) aa group by week
    union
    select 0 week,0 num
    union
    select 1 week,0 num
    union
    select 2 week,0 num
    union
    select 3 week,0 num
    union
    select 4 week,0 num
    union
    select 5 week,0 num
    union
    select 6 week,0 num) aa
    group by week
4  月报表

select time , sum(num) total from (
  select DATE_FORMAT(problem_find_time,'%d')  time ,count(problem_find_time) num
    from problem_feedback where problem_find_time is not null
    and
    problem_state in
    <foreach collection="state" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    and
    DATE_FORMAT(problem_find_time,'%Y-%m') = #{time} and
    department_id in
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    group by DATE_FORMAT(problem_find_time,'%Y-%m-%d')
 
    UNION SELECT '01' as time,'0' as sum
        UNION SELECT '02' as time,'0' as sum
        UNION SELECT '03' as time,'0' as sum
        UNION SELECT '04' as time,'0' as sum
        UNION SELECT '05' as time,'0' as sum
        UNION SELECT '06' as time,'0' as sum
        UNION SELECT '07' as time,'0' as sum
        UNION SELECT '08' as time,'0' as sum
        UNION SELECT '09' as time,'0' as sum
        UNION SELECT '10' as time,'0' as sum
        UNION SELECT '11' as time,'0' as sum
        UNION SELECT '12' as time,'0' as sum
        UNION SELECT '13' as time,'0' as sum
        UNION SELECT '14' as time,'0' as sum
        UNION SELECT '15' as time,'0' as sum
        UNION SELECT '16' as time,'0' as sum
        UNION SELECT '17' as time,'0' as sum
        UNION SELECT '18' as time,'0' as sum
        UNION SELECT '19' as time,'0' as sum
        UNION SELECT '20' as time,'0' as sum
        UNION SELECT '21' as time,'0' as sum
        UNION SELECT '22' as time,'0' as sum
        UNION SELECT '23' as time,'0' as sum
        UNION SELECT '24' as time,'0' as sum
        UNION SELECT '25' as time,'0' as sum
        UNION SELECT '26' as time,'0' as sum
        UNION SELECT '27' as time,'0' as sum
        UNION SELECT '28' as time,'0' as sum
        UNION SELECT '29' as time,'0' as sum
        UNION SELECT '30' as time,'0' as sum
        UNION SELECT '31' as time,'0' as sum )aa group by time
  </select>
这是我们计算报表常用sql

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值