SELECT IQ, PARENT_EQ_NAME1, REASON_NAME FROM (
SELECT SUM(IQ) AS IQ, PARENT_EQ_NAME1, REASON_NAME1 AS REASON_NAME FROM
(
SELECT REASON_NAME, IQ, PARENT_EQ_NAME1, rank1,
CASE WHEN rank1 > 5 THEN 'SUM OF REMAINING' ELSE REASON_NAME END REASON_NAME1
FROM (
SELECT REASON_NAME, IQ, PARENT_EQ_NAME1, RANK() OVER(PARTITION BY PARENT_EQ_NAME1
ORDER BY IQ DESC) AS rank1
FROM (
SELECT SUM(TH.IMPACT_QTY ) IQ,TH.PARENT_EQ_NAME1,TH.REASON_NAME FROM
(
select impact_qty,eq_name PARENT_EQ_NAME1,reason_name from c_maaden_vw_losses_by_area where PARENT_Eq_name=:parenteqName
AND (dvtn_start_dt_tm >= :StartDate)
AND (dvtn_end_dt_tm <= :EndDate) AND (TRGT_TYPE IN (:Type))
-- eq_name
UNION ALL
select impact_qty,parent_eq_name PARENT_EQ_NAME1, reason_name from c_maaden_vw_losses_by_area where
parent_Eq_name in (select distinct eq_name from ip_equip_hierarchy where parent_eq_name =:parenteqName )
AND (dvtn_start_dt_tm >= :StartDate)
AND (dvtn_end_dt_tm <= :EndDate) AND (TRGT_TYPE IN (:Type)) -- parent_Eq_name
UNION ALL
SELECT CA.IMPACT_QTY,
Case
when IH.parent_eq_name is null then CA.parent_eq_name
else IH.parent_eq_name end parent_EQ_NAME1,reason_name
FROM C_MAADEN_VW_LOSSES_BY_AREA CA left outer join
(select distinct t1.parent_eq_name GP_eq_name ,t2.parent_eq_name , T2.Eq_name from ip_equip_hierarchy T1, ip_equip_hierarchy T2
where T1.eq_name=t2.Parent_EQ_name and t1.parent_eq_name= :parenteqName and T1.hierarchy_typ='ASSET') IH
on CA.parent_eq_name=IH.eq_name
where CA.parent_Eq_name
in (select distinct eq_name from ip_equip_hierarchy where parent_eq_name in (select distinct eq_name
from ip_equip_hierarchy
where parent_Eq_name = :parenteqName ) ) AND (dvtn_start_dt_tm >= :StartDate)
AND (dvtn_end_dt_tm <= :EndDate) AND (TRGT_TYPE IN (:Type))
) TH
where NOT(TH.REASON_NAME='Other' AND TH.PARENT_EQ_NAME1 = :parenteqName )
AND (TH.REASON_NAME NOT IN ('DIGESTION SLURRY FEED PUMP', 'CAB TEST OR ACID CLEANING'))
GROUP BY TH.PARENT_EQ_NAME1,TH.REASON_NAME
)))
GROUP BY PARENT_EQ_NAME1, REASON_NAME1
union all
SELECT SUM(IMPACT_QTY) AS IQ,:parenteqName as PARENT_EQ_NAME1, REASON_NAME from C_MAADEN_VW_LOSSES_BY_AREA
where eq_name=:parenteqName and REASON_NAME= 'Other' AND (dvtn_start_dt_tm >= :StartDate)
AND (dvtn_end_dt_tm <= :EndDate) AND (TRGT_TYPE IN (:Type))
group by PARENT_EQ_NAME,REASON_NAME ) order by IQ DESC
答案 0 :(得分:0)
我希望你期望用上面的查询编写简单的程序。所以我用你的SQL查询创建了如下过程。
程序将创建。但是没有使用这个程序。
Create procedure p(parenteqName Varchar2(30),
StartDate Date,
EndDate Date,
Type Varchar2(10))
V_IQ Number(10);
V_ PARENT_EQ_NAME1 varchar2(30);
V_ REASON_NAME varchar2(30);
Begin
SELECT IQ, PARENT_EQ_NAME1, REASON_NAME into V_IQ , V_ PARENT_EQ_NAME1, V_ REASON_NAME FROM (
SELECT SUM(IQ) AS IQ, PARENT_EQ_NAME1, REASON_NAME1 AS REASON_NAME FROM
(
SELECT REASON_NAME, IQ, PARENT_EQ_NAME1, rank1,
CASE WHEN rank1 > 5 THEN 'SUM OF REMAINING' ELSE REASON_NAME END REASON_NAME1
FROM (
SELECT REASON_NAME, IQ, PARENT_EQ_NAME1, RANK() OVER(PARTITION BY PARENT_EQ_NAME1
ORDER BY IQ DESC) AS rank1
FROM (
SELECT SUM(TH.IMPACT_QTY ) IQ,TH.PARENT_EQ_NAME1,TH.REASON_NAME FROM
(
select impact_qty,eq_name PARENT_EQ_NAME1,reason_name from c_maaden_vw_losses_by_area where PARENT_Eq_name= parenteqName
AND (dvtn_start_dt_tm >= StartDate)
AND (dvtn_end_dt_tm <= EndDate) AND (TRGT_TYPE IN (Type))
-- eq_name
UNION ALL
select impact_qty,parent_eq_name PARENT_EQ_NAME1, reason_name from c_maaden_vw_losses_by_area where
parent_Eq_name in (select distinct eq_name from ip_equip_hierarchy where parent_eq_name =parenteqName )
AND (dvtn_start_dt_tm >= StartDate)
AND (dvtn_end_dt_tm <= EndDate) AND (TRGT_TYPE IN (Type)) -- parent_Eq_name
UNION ALL
SELECT CA.IMPACT_QTY,
Case
when IH.parent_eq_name is null then CA.parent_eq_name
else IH.parent_eq_name end parent_EQ_NAME1,reason_name
FROM C_MAADEN_VW_LOSSES_BY_AREA CA left outer join
(select distinct t1.parent_eq_name GP_eq_name ,t2.parent_eq_name , T2.Eq_name from ip_equip_hierarchy T1, ip_equip_hierarchy T2
where T1.eq_name=t2.Parent_EQ_name and t1.parent_eq_name= parenteqName and T1.hierarchy_typ='ASSET') IH
on CA.parent_eq_name=IH.eq_name
where CA.parent_Eq_name
in (select distinct eq_name from ip_equip_hierarchy where parent_eq_name in (select distinct eq_name
from ip_equip_hierarchy
where parent_Eq_name = parenteqName ) ) AND (dvtn_start_dt_tm >= StartDate)
AND (dvtn_end_dt_tm <= EndDate) AND (TRGT_TYPE IN (Type))
) TH
where NOT(TH.REASON_NAME='Other' AND TH.PARENT_EQ_NAME1 = parenteqName )
AND (TH.REASON_NAME NOT IN ('DIGESTION SLURRY FEED PUMP', 'CAB TEST OR ACID CLEANING'))
GROUP BY TH.PARENT_EQ_NAME1,TH.REASON_NAME
)))
GROUP BY PARENT_EQ_NAME1, REASON_NAME1
union all
SELECT SUM(IMPACT_QTY) AS IQ,parenteqName as PARENT_EQ_NAME1, REASON_NAME from C_MAADEN_VW_LOSSES_BY_AREA
where eq_name=parenteqName and REASON_NAME= 'Other' AND (dvtn_start_dt_tm >= StartDate)
AND (dvtn_end_dt_tm <= EndDate) AND (TRGT_TYPE IN (Type))
group by PARENT_EQ_NAME,REASON_NAME ) order by IQ DESC
Exception
When others then
Null;
End;
最好在这里提问之前先谷歌。