关于统计报表常用sql

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zly145236/article/details/85159870

 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

猜你喜欢

转载自blog.csdn.net/zly145236/article/details/85159870