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') = # and
department_id in
#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
#and
date_format(problem_find_time,'%y-%m-%d') = # and
department_id in
#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
)aagroup 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
andproblem_state in
#and
date_format(problem_find_time,'%y-%m-%d') between # and # and
department_id in
#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
andproblem_state in
#and
date_format(problem_find_time,'%y-%m') = # and
department_id in
#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
這是我們計算報表常用sql
測試統計報表SQL常用總結
插入錶值 insert into values insert into 表名x 列名1 列名2 列名3 values where條件剔除與匹配 where x.nvl biz cd,0 notin 2817 2818 存在空值時用nvl補全 and x.bank acct num not like ...
統計報表 sql統計語句
需要資料統計頁面,肯定需要匯出資料,於是,邊學邊寫,完成了一段sql 最早的版本是這樣的 分三條sql查出三種不同的狀態的記錄數 總記錄,未支付,已支付 select count as recordcount from t record where createtime 2019 01 01 00 ...
報表統計 sql面試題
有3張表 學生表 s 字段 學生編號 學生姓名 課程表 c 字段 課程編號 課程名稱 成績表 sc 字段 學生編號 課程編號 分數 需要實現最終效果如下 後面有些課程省略沒有截圖了 實現的方法 方法一 select max s.name as姓名,max case when sc.cid 1 the...