Select l.m_names, TO_CHAR(SUM(Cou_NP_OKS)) "ОКС_Случаев",TO_CHAR(SUM(SUM_NP_OKS)) "ОКС_Сумма",TO_CHAR(SUM(Cou_NP_ONMK)) "ОНМК_Случаев",TO_CHAR(SUM(SUM_NP_ONMK)) "ОНМК_Сумма"
From
(
Select
sl.lpu_id,
Case when (Sl.Lpu_Id Not in (1,11,20,198,188)) and (SUBSTR(SL.DS1,1,3)='I21' or SL.DS1='I20.0') Then 1 Else Null end Cou_NP_OKS,
Case when (Sl.Lpu_Id Not in (1,11,20,198,188)) and (SUBSTR(SL.DS1,1,3)='I21' or SL.DS1='I20.0') Then z.SUMP Else Null end SUM_NP_OKS,
Case when (Sl.Lpu_Id Not in (1,11,20,198,188)) and (SUBSTR(SL.DS1,1,3) in ('I60','I61','I62','I63','I64') or SUBSTR(SL.DS1,1,3) in ('G45','G46')) Then 1 Else Null end Cou_NP_ONMK,
Case when (Sl.Lpu_Id Not in (1,11,20,198,188)) and (SUBSTR(SL.DS1,1,3) in ('I60','I61','I62','I63','I64') or SUBSTR(SL.DS1,1,3) in ('G45','G46')) Then z.SUMP Else Null end SUM_NP_ONMK
From MTR.S19_SL SL, MTR.S19_ZAP Z
Where SL.Code In (Select CODE FROM MTR.S19_schet WHERE YEAR=$SY$ and month between $SM$ and $EM$)
and z.SUMP>0 and z.USL_OK=1 and sl.id_zap = z.id_zap and
(
(SUBSTR(SL.DS1,1,3)='I21' or SL.DS1='I20.0') or
(SUBSTR(SL.DS1,1,3) in ('I60','I61','I62','I63','I64') or SUBSTR(SL.DS1,1,3) in ('G45','G46')))
) SL1, MTR.LPU L
Where Sl1.Lpu_Id=L.LPU_ID
group by l.m_names
