格式:
select 字段1,字段2
from 表名
where 條件
group by 字段
樣例一:
1.需要每個市的對應數據
-- 計算 審批完成時間和提交審批時間天數(總時間差) 總數據量 行政區划
select sum(TIMESTAMPDIFF(day,jdjsprq,jspwcrq)) as zsj,count(1) as zsl,substr(JXZJGBH,1,4) xzqh
from sp_jl b
where b.jzt=1 and jdjsprq like '2019%' and jspwcrq like '2019%'
group by substr(JXZJGBH,1,4)
2.各個地市的審批時效,審批時效:總的審批時間/總的審批數量
select d.xjgmc,round(zsj/zsl,2) as spsx from (
-- 計算 審批完成時間和提交審批時間天數 總數據量 行政區划
select sum(TIMESTAMPDIFF(day,jdjsprq,jspwcrq)) as zsj,count(1) as zsl,substr(JXZJGBH,1,4) xzqh
from sp_jl b
where b.jzt=1 and jdjsprq like '2019%' and jspwcrq like '2019%' and INSTR('db_jz,kn_jz,wb_jz,ls_jz',b.JSPXMBM)>0
-- 過濾單據類型 審批狀態 和日期
group by substr(JXZJGBH,1,4)) c,mz_xzjg d
where c.xzqh=d.node_id
樣例二:
對組進行過濾,having的使用
SELECT
distinct(pk_sr_main)
FROM
sr_main a,
sr_detail b
WHERE
a.pk_sr_main = b.fk_sr_main
AND a.mdjlx = 'ls_jz'
AND b.dsfxs = '01'
AND b.dryxxlb = 'jt'
and a.sys_spzt = 1
and a.sys_djzt = 1
GROUP BY
fk_sr_main
HAVING
max(mxsrs) <> count(pk_sr_detail);
where與having的區別:where是對行進行過濾,having是對組進行過濾;