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
继续阅读与本文标签相同的文章
-
什么是DDNS
2026-05-14栏目: 教程
-
华为的鸿蒙系统和苹果的操作系统有什么区别?又有什么共同点呢?
2026-05-14栏目: 教程
-
中国工程院院士谈5G辐射:5G基站更密集 比微波炉辐射更低
2026-05-14栏目: 教程
-
当年那个说要“毁灭人类”的女机器人,如今怎么样了呢?变化真大
2026-05-14栏目: 教程
-
无法复制网页上的文本?学会这5种方法,轻松免费复制
2026-05-14栏目: 教程
