我想找到每组的最大值,
首先找到包含不同系列的组的计数(*)。
select count(*) cnt,f.org_name org_name ,e.err_type||'>>'||s.sub_type error
from MOE_OFFICE f ,MOE_COMPLAINT c ,MOE_ANSWERS a ,moe_error e ,moe_sub_error s
where f.MAIN_OFFICE=:F200_X
and f.ORG_ID = c.com_to
and c.com_id = a.com_id
and a.err_id = e.err_id
and a.sub_id = s.sub_id
and e.err_id = s.sub_err_id
group by (e.err_type||'>>'||s.sub_type),f.org_name
having count(*) = ( select max(cnt) from
(select count(*) cnt
from MOE_OFFICE f ,MOE_COMPLAINT c ,MOE_ANSWERS a ,moe_error e ,moe_sub_error s
where f.MAIN_OFFICE=:F200_X
and f.ORG_ID = c.com_to
and c.com_id = a.com_id
and a.err_id = e.err_id
and a.sub_id = s.sub_id
and e.err_id = s.sub_err_id
group by (e.err_type||'>>'||s.sub_type),f.org_name
)
) order by 1 desc
注意: - F200_X是动态变量,可以是10或20或30
答案 0 :(得分:0)
WITH MAIN_QUERY AS
(SELECT COUNT(*) CNT
, (E.ERR_TYPE || '>>' || S.SUB_TYPE) AS THE_ERROR
, F.ORG_NAME
FROM MOE_OFFICE F
, MOE_COMPLAINT C
, MOE_ANSWERS A
, MOE_ERROR E
, MOE_SUB_ERROR S
WHERE F.MAIN_OFFICE = :F200_X
AND F.ORG_ID = C.COM_TO
AND C.COM_ID = A.COM_ID
AND A.ERR_ID = E.ERR_ID
AND A.SUB_ID = S.SUB_ID
AND E.ERR_ID = S.SUB_ERR_ID
GROUP BY (E.ERR_TYPE || '>>' || S.SUB_TYPE)
, F.ORG_NAME)
SELECT MAX(CNT)
, THE_ERROR
, ORG_NAME
FROM MAIN_QUERY
GROUP BY THE_ERROR
, ORG_NAME
ORDER BY 1 DESC