How Optimize Sum SQL Query? - sql

I have this query.
select a.NoSPKJahit as 'TglSPKJahit',
c.TglSPKJahit as 'spkJahit',
a.SeriBarang as 'KodeSeri',
b.NamaBarang as 'NamaBarang',
a.JmlTotalPotong-a.JmlTotalRusakSablon as 'JumlahSPKJahit',
a.JmlTotalSelesaiJahit as 'JumlahHasilJahit',
(select sum(Qty) from PenjualanDTL where KodeBarang = a.KodeBarang) as 'JumlahPenjualan',
(select sum(JumlahRetur) from StokBS where KodeBarang = a.KodeBarang) as 'JumlahRetur',
(select sum(JumlahRusak) from StokBS where KodeBarang = a.KodeBarang) as 'JumlahRusak',
(a.JmlTotalSelesaiJahit - (select sum(Qty) from PenjualanDTL where KodeBarang = a.KodeBarang) +((select sum(JumlahRetur) from StokBS where KodeBarang = a.KodeBarang)-(select sum(JumlahRusak) from StokBS where KodeBarang = a.KodeBarang))) as 'SisaBarang',
(select sum(JumlahStok) from StokToko where KodeBarang = a.KodeBarang and KodeToko = 'GD000')as 'Gudang',
(select sum(JumlahStok) from StokToko where KodeBarang = a.KodeBarang and KodeToko = 'GD001')as 'GudangAtas',
(select sum(JumlahStok) from StokToko where KodeBarang = a.KodeBarang and KodeToko = 'GD002')as 'Mobil',
(select sum(JumlahStok) from StokToko where KodeBarang = a.KodeBarang and KodeToko = 'OL01')as 'MissMode',
(select sum(JumlahStok) from StokToko where KodeBarang = a.KodeBarang and KodeToko = 'TK005')as 'SilverLeafM'
from DetilBarang a
left join MsBarang b on b.KodeBarang = a.KodeBarang
left join SPKJahit c on c.NoSPKJahit = a.NoSPKJahit
but have very slow performace. How can i speed up query performance ?
Thanks.

I'd move the subqueries to the join clause and use conditional aggregation (SUM(CASE WHEN ...) for all the StokToko.JumlahStok sums.
select
db.NoSPKJahit as "TglSPKJahit",
sj.TglSPKJahit as "spkJahit",
db.SeriBarang as "KodeSeri",
mb.NamaBarang as "NamaBarang",
db.JmlTotalPotong - db.JmlTotalRusakSablon as "JumlahSPKJahit",
db.JmlTotalSelesaiJahit as "JumlahHasilJahit",
pd."JumlahPenjualan",
sb."JumlahRetur",
sb."JumlahRusak",
db.JmlTotalSelesaiJahit - pd."JumlahPenjualan" + sb."JumlahRetur" - sb."JumlahRusak"
as "SisaBarang",
st."Gudang",
st."GudangAtas",
st."Mobil",
st."MissMode",
st."SilverLeafM"
from DetilBarang db
left join MsBarang mb on mb.KodeBarang = db.KodeBarang
left join SPKJahit sj on sj.NoSPKJahit = db.NoSPKJahit
left join
(
select
KodeBarang,
sum(Qty) as "JumlahPenjualan"
from PenjualanDTL
group by KodeBarang
) pd on pd.KodeBarang = db.KodeBarang
left join
(
select
KodeBarang,
sum(JumlahRetur) as "JumlahRetur",
sum(JumlahRusak) as "JumlahRusak"
from StokBS
group by KodeBarang
) sb on sb.KodeBarang = db.KodeBarang
left join
(
select
KodeBarang,
sum(case when KodeToko = 'GD000' then JumlahStok end) as "Gudang",
sum(case when KodeToko = 'GD001' then JumlahStok end) as "GudangAtas",
sum(case when KodeToko = 'GD002' then JumlahStok end) as "Mobil",
sum(case when KodeToko = 'OL01' then JumlahStok end) as "MissMode",
sum(case when KodeToko = 'TK005' then JumlahStok end) as "SilverLeafM"
from StokToko
group by KodeBarang
) st on st.KodeBarang = db.KodeBarang
order by db.NoSPKJahit;
Please change the left outer joins to inner joins where appropriate.
Recommended indexes to speed up the query:
create index idx1 on msbarang (kodebarang, namabarang);
create index idx2 on spkjahit (nospkjahit, tglspkjahit);
create index idx3 on penjualandtl (kodebarang, qty);
create index idx4 on stokbs (kodebarang, jumlahretur, jumlahrusak);
create index idx5 on stoktoko (kodebarang, kodetoko, jumlahstok);
This may be as fast as it gets. Without any WHERE clause you are probably querying a lot of data.

you can try this
SELECT A.*, sum(ISNULL(B.qty, 0)) as TotQty
from SiteInformation A
left join ItemSite B on A.SiteID = B.SiteID and A.InvtID = B.InvtID
I Think it will work better than TotQty = (SELECT SUM(Qty) FROM where...)

Related

How can I remove the the union from this code?

I am attempting to remove the union in this sub-select due to performance issues. All attempts are unsuccessful because of how the AllocatedCRGReservedQty is calculated. How can I remove the union and combine the 2 select statements?
I was able to remove the union on the other portion of the query because it was based on a single where clause difference.
SELECT SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,InvQty
,SrdQty
,SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,AllocatedCRGReservedQty
FROM
( SELECT DISTINCT
SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,AsOfInventory InvQty
,SUM(SrdQty)
OVER (PARTITION BY SrdKey) SrdQty
,SUM(SrdSampleApprovedQty)
OVER (PARTITION BY SrdKey) SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,(SELECT SUM(SltLotAllocTranQty)
FROM SalesOrderLot
INNER JOIN
SalesOrderLotTran ON SltSolKey = SolKey
WHERE SolCompanyID = SodCompanyID AND
SolBranchID = SodBranchID AND
SolSalesOrder = SodSalesOrder AND
SolSalesOrderDtlKey = SodSalesOrderDtlKey AND
SolWipFgLotID = SrdLotID) AllocatedCRGReservedQty
FROM SampleRequestHdr
INNER JOIN SampleRequestDtl ON SrdSrhKey = SrhKey
INNER JOIN Warehouse ON WhsWhseID = SrhWhseID
INNER JOIN Lot ON LotCompanyID = SrhCompanyID AND
LotItemID = SrdItemID AND
LotID = SrdLotID
INNER JOIN #AsOfInventory ON IntLotKey = LotKey
-- Only include inventory transactions for completed production orders
-- Changed to temptable due to deadlocks on prodorder/prodorderreservation
INNER JOIN SalesOrderDtl ON SodCompanyID = SrhCompanyID AND
SodBranchID = SrdBranchID AND
SodSalesOrder = SrdSalesOrder AND
SodSalesOrderDtlKey = SrdSalesOrderDtlKey
INNER JOIN SalesOrderHdr ON SohCompanyID = SodCompanyID AND
SohBranchID = SodBranchID AND
SohSalesOrder = SodSalesOrder
WHERE SrhCompanyID = #CompanyID AND
((#PlantID <> '*' AND WhsPlantID = #PlantID) OR (#PlantID = '*')) AND
SrhApprovalExpireDate >= #As_Of_Date AND
ISNULL(SrdSampleRejected,'N') = 'N' AND
SohType IN ('Contract','Normal') AND
SrdQty <> 0
) WORK
WHERE ISNULL(AllocatedCRGReservedQty,0) < SrdQty -- if the lot is already allocated for at least the offered qty then don't include it so we don't over-state the gallons offered
UNION ALL -- then get the sample request qty offered for non-contract sales orders or no sales order assigned
SELECT SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,InvQty
,SrdQty
,SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,AllocatedCRGReservedQty
FROM
( SELECT DISTINCT
SrhCompanyID
,WhsPlantID
,SrhReason
,SrhRequestID
,SrdSeq
,SrdLotID
,SrdItemID
,AsOfInventory InvQty
,SUM(SrdQty)
OVER (PARTITION BY SrdKey) SrdQty
,SUM(SrdSampleApprovedQty)
OVER (PARTITION BY SrdKey) SrdSampleApprovedQty
,SrdUOM
,SohSalesOrder
,SohType
,(SELECT SUM(WptTranQty)
FROM WineProgram
CROSS APPLY
FN_CDV_WineProgramLotTranPlusPending(SrdLotID, WpgKey)
INNER JOIN
IntendedUse ON InuKey = WpgInuKey AND
InuIntendedUse = 'CRG'
LEFT JOIN
--ProdOrdDtl (NOLOCK) ON PodKey = wptPodKey
va_CDV_ProdOrder ON PodKey = wptPodKey
WHERE --WptLotID = SrdLotID AND
ISNULL(WptTranType,'') <> 'Pending' AND
((wptPodKey IS NOT NULL AND
(#CompletePOsOnly = 'N' OR -- include all POs
(#CompletePOsOnly = 'Y' AND PohCompleteDate IS NOT NULL))) -- Only include transactions if PO is Complete or if it's a non-PO transaction
OR
wptPodKey IS NULL
)
) AllocatedCRGReservedQty
FROM SampleRequestHdr
INNER JOIN SampleRequestDtl ON SrdSrhKey = SrhKey
INNER JOIN Warehouse ON WhsWhseID = SrhWhseID
INNER JOIN Lot ON LotCompanyID = SrhCompanyID AND
LotItemID = SrdItemID AND
LotID = SrdLotID
INNER JOIN
#AsOfInventory ON IntLotKey = LotKey
LEFT JOIN SalesOrderDtl ON SodCompanyID = SrhCompanyID AND
SodBranchID = SrdBranchID AND
SodSalesOrder = SrdSalesOrder AND
SodSalesOrderDtlKey = SrdSalesOrderDtlKey
LEFT JOIN SalesOrderHdr ON SohCompanyID = SodCompanyID AND
SohBranchID = SodBranchID AND
SohSalesOrder = SodSalesOrder
WHERE SrhCompanyID = #CompanyID AND
((#PlantID <> '*' AND WhsPlantID = #PlantID) OR (#PlantID = '*')) AND
SrhApprovalExpireDate >= #As_Of_Date AND
ISNULL(SrdSampleRejected,'N') = 'N' AND
ISNULL(SrdUseCRGReservation,'N') = 'N' AND -- if the SrdUseCRGReservation flag = "N" then return the sample qty
ISNULL(SohType,'') NOT IN ('Contract','Normal') AND -- offered to reduce the ATP (pseudo reservation until the sample expires or is approved/rejected)
SrdQty <> 0
) WORK

Why does the subquery throw errors in a group by query?

Ok, I will try a 2nd attempt because the first one was not that intelligent
My Query:
SELECT distinct kg.datum,
sum(case when leis.code = 'Oph3001' then leis.anzahl END) as leis3001,
sum(case when leis.code = 'Oph3003' then leis.anzahl END) as leis3003,
(select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual) as real_datum
FROM kg_eintraege kg
INNER JOIN aufenthalte a
ON kg.patient_nr = a.patient_nr
and kg.fall_nr = a.fall_nr
INNER JOIN MF_LEIS_DIAG_OP_MD leis
on leis.aufenthalte_nr = a.nr
group by kg.datum, kg.kg_id
order by /*real_datum*/ kg.datum desc
The following subquery is causing the problem:
(select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual)
Puting it into the group by expression throws the ORA-22818 error ("subquery not allowed here").
Not puting it throws the ORA-00979 error ("not a group by expression").
Can someone help me?
You could use an outer join instead; something like:
SELECT distinct kg.datum,
sum(case when leis.code = 'Oph3001' then leis.anzahl END) as leis3001,
sum(case when leis.code = 'Oph3003' then leis.anzahl END) as leis3003,
to_date(KG2.KURZTEXT, 'dd.mm.yyyy') as real_datum
FROM kg_eintraege kg
INNER JOIN aufenthalte a
ON kg.patient_nr = a.patient_nr
and kg.fall_nr = a.fall_nr
INNER JOIN MF_LEIS_DIAG_OP_MD leis
on leis.aufenthalte_nr = a.nr
LEFT OUTER JOIN kg_eintraege kg2
on kg2.kgtitel_nr = 1003350007
and kg.fall_nr = kg2.fall_nr
and kg.patient_nr = kg2.patient_nr
and kg.kg_id = kg2.kontext
group by kg.datum, kg.kg_id, to_date(KG2.KURZTEXT, 'dd.mm.yyyy')
order by /*real_datum*/ kg.datum desc
you can use a subquery
select datum,
sum(case when code = 'Oph3001' then anzahl END) as leis3001,
sum(case when code = 'Oph3003' then anzahl END) as leis3003,
real_datum
from (
SELECT distinct kg.datum,
leis.code,
leis.anzahl,
(select nvl((select to_date(KG2.KURZTEXT, 'dd.mm.yyyy')from kg_eintraege kg2 where kg2.kgtitel_nr = 1003350007 and kg.fall_nr = kg2.fall_nr and kg.patient_nr = kg2.patient_nr and kg.kg_id = kg2.kontext),'') from dual) as real_datum
FROM kg_eintraege kg
INNER JOIN aufenthalte a ON kg.patient_nr = a.patient_nr
and kg.fall_nr = a.fall_nr
INNER JOIN MF_LEIS_DIAG_OP_MD leis on leis.aufenthalte_nr = a.nr
)
group by datum, kg_id, real_datum
order by /*real_datum*/ kg.datum desc

SQL Query - How to suppress repeating values in the result set?

I'm trying to suppress the repeating values in TotalCarton column. Have tried to replace the value either blank or null but went failed. Any help?
Here is the SQL Script:
SELECT ORDERS.StorerKey,
ORDERS.OrderKey,
PackKey = (SELECT MAX(PackKey) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE ORderKey = ORDERS.OrderKey),
PackHU = BAX_PACK_DTL.OuterPackID,
SalesOrderNum = ( SELECT Upper(Max(ORDERDETAIL.CustShipInst01)) FROM ORDERDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey),
DeliveryNum = Upper(ORDERS.ExternOrderKey),
TotalCarton = ( CASE BAX_PACK_DTL.PackType WHEN 'C' THEN Count(DISTINCT(BAX_PACK_DTL.OuterPackID))
ELSE 0 END ),
TotalPallet = ( CASE BAX_PACK_DTL.PackType WHEN 'P' THEN Count(DISTINCT(BAX_PACK_DTL.OuterPackID))
ELSE 0 END ),
SumCarton = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE PackType = 'C' AND PackKey = '0000000211'),
SumPallet = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE PackType = 'P' AND PackKey = '0000000211'),
AddWho = Upper(ORDERS.EditWho),
ORDERS.AddDate
FROM ORDERS WITH (NOLOCK) INNER JOIN ORDERDETAIL WITH (NOLOCK) ON ORDERS.StorerKey = ORDERDETAIL.StorerKey
AND ORDERS.OrderKey = ORDERDETAIL.OrderKey
INNER JOIN PICKDETAIL WITH (NOLOCK) ON ORDERDETAIL.StorerKey = PICKDETAIL.StorerKey
AND ORDERDETAIL.OrderKey = PICKDETAIL.OrderKey
AND ORDERDETAIL.OrderLineNumber = PICKDETAIL.OrderLineNumber
INNER JOIN BAX_PACK_DTL WITH (NOLOCK) ON PICKDETAIL.OrderKey = BAX_PACK_DTL.OrderKey
AND PICKDETAIL.PickDetailKey = BAX_PACK_DTL.PickDetailKey
WHERE (SELECT COUNT(DISTINCT(ORDERKEY)) FROM PICKDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey ) > 0
AND BAX_PACK_DTL.PackKey = '0000000211'
AND BAX_PACK_DTL.OuterPackID IN
('P111111111',
'P22222222',
'P33333333')
GROUP BY ORDERS.StorerKey,
ORDERS.OrderKey,
ORDERS.ExternOrderKey,
ORDERS.HAWB,
ORDERS.SO,
ORDERS.EditWho,
ORDERS.AddDate,
PICKDETAIL.WaveKey,
BAX_PACK_DTL.OuterPackID,
BAX_PACK_DTL.PackKey,
BAX_PACK_DTL.PackType
ORDER BY BAX_PACK_DTL.OuterPackID ASC
Below is the current result set based on the query above.
Your code looks really strange. I would expect the query to use conditional aggregation and look more like this:
SELECT ORDERS.StorerKey, ORDERS.OrderKey,
PackKey = (SELECT MAX(PackKey) FROM BAX_PACK_DTL WITH (NOLOCK) WHERE ORderKey = ORDERS.OrderKey),
PackHU = BAX_PACK_DTL.OuterPackID,
SalesOrderNum = ( SELECT Upper(Max(ORDERDETAIL.CustShipInst01)) FROM ORDERDETAIL WITH (NOLOCK) WHERE OrderKey = ORDERS.OrderKey),
DeliveryNum = Upper(ORDERS.ExternOrderKey),
TotalCarton = COUNT(DISTINCT CASE BAX_PACK_DTL.PackType WHEN 'C' THEN BAX_PACK_DTL.OuterPackID END),
TotalPallet = COUNT(DISTINCT CASE BAX_PACK_DTL.PackType WHEN 'P' THEN BAX_PACK_DTL.OuterPackID END),
SumCarton = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL bpd WHERE pbd.PackType = 'C' AND pbd.PackKey = '0000000211'),
SumPallet = (SELECT COUNT(DISTINCT(OuterPackSeq)) FROM BAX_PACK_DTL bpd WHERE pbd.PackType = 'P' AND pbd.PackKey = '0000000211'),
AddWho = Upper(ORDERS.EditWho),
ORDERS.AddDate
FROM . . .
GROUP BY ORDERS.StorerKey, ORDERS.OrderKey, Upper(ORDERS.ExternOrderKey),
Upper(ORDERS.EditWho), ORDERS.AddDate;
This may not be exact. You have not qualified column names, given the table structure, and are using very arcane query syntax, mixing subqueries and aggregations. But it should give an idea.

Selecting rows with MAX(Column value), DISTINCT by another column in SQL

I have two tables that I'm having difficulty with. OrderHed & UD11. OrderHed has 1 value for OrderNum=70960, but UD11 has 4 records for 70960.
I'm trying to return the max value of UD11.Key5 as a distinct record - what am I doing wrong?
Thanks!
SELECT TOP (100) PERCENT OrderHed.OrderNum,
OrderHed.OpenOrder,
OrderHed.OrderDate,
OrderRel.Plant,
OrderHed.EntryPerson,
OrderHed.Company,
Erp.Customer.CustID,
Erp.Customer.NAME,
Erp.InvcDtl.InvoiceNum,
SUM(OrderDtl.ExtPriceDtl) AS Expr1,
OrderHed.PONum,
Ice.UD11.Key1,
Ice.UD11.Key2,
Ice.UD11.Key3,
Ice.UD11.Key4,
MAX(Ice.UD11.Key5) AS Expr2,
Ice.UD11.Character01,
Ice.UD11.Number01,
Ice.UD11.Date01,
Ice.UD11.Date02,
Ice.UD11.Date03,
Ice.UD11.ShortChar01,
Ice.UD11.ShortChar02,
Ice.UD11.ShortChar03,
Ice.UD11.ShortChar04,
Ice.UD11.ShortChar05,
OrderHed.OrderComment
FROM Erp.Customer
RIGHT JOIN Ice.UD11
RIGHT JOIN Erp.OrderHed AS OrderHed ON Ice.UD11.Company = OrderHed.Company
AND Ice.UD11.Key1 = OrderHed.OrderNum
LEFT JOIN Erp.OrderRel AS OrderRel
RIGHT JOIN Erp.OrderDtl AS OrderDtl ON OrderRel.Company = OrderDtl.Company
AND OrderRel.OrderNum = OrderDtl.OrderNum
AND OrderRel.OrderLine = OrderDtl.OrderLine ON OrderHed.Company = OrderDtl.Company
AND OrderHed.OrderNum = OrderDtl.OrderNum ON Erp.Customer.Company = OrderHed.Company
AND Erp.Customer.CustNum = OrderHed.CustNum LEFT JOIN Erp.InvcDtl RIGHT JOIN Erp.ShipDtl ON Erp.InvcDtl.Company = Erp.ShipDtl.Company
AND Erp.InvcDtl.OrderNum = Erp.ShipDtl.OrderNum
AND Erp.InvcDtl.OrderLine = Erp.ShipDtl.OrderLine
AND Erp.InvcDtl.OrderRelNum = Erp.ShipDtl.OrderRelNum ON OrderRel.Company = Erp.ShipDtl.Company
AND OrderRel.OrderNum = Erp.ShipDtl.OrderNum
AND OrderRel.OrderLine = Erp.ShipDtl.OrderLine
AND OrderRel.OrderRelNum = Erp.ShipDtl.OrderRelNum GROUP BY OrderHed.OrderNum,
OrderHed.OrderDate,
OrderRel.Plant,
OrderHed.EntryPerson,
OrderHed.Company,
OrderHed.OpenOrder,
Erp.Customer.CustID,
Erp.Customer.NAME,
Erp.InvcDtl.InvoiceNum,
OrderHed.PONum,
Ice.UD11.Key1,
Ice.UD11.Key2,
Ice.UD11.Key3,
Ice.UD11.Key4,
Ice.UD11.Character01,
Ice.UD11.Number01,
Ice.UD11.Date01,
Ice.UD11.Date02,
Ice.UD11.Date03,
Ice.UD11.ShortChar01,
Ice.UD11.ShortChar02,
Ice.UD11.ShortChar03,
Ice.UD11.ShortChar04,
Ice.UD11.ShortChar05,
OrderHed.OrderComment HAVING (OrderHed.Company = N'011')
AND (Erp.InvcDtl.InvoiceNum IS NULL)
AND (OrderHed.OrderNum = 70960)
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY OrderNum ORDER BY key5 DESC) rn
FROM (... /* my huge query */) q
) q
WHERE rn = 1

ORA-01799: a column may not be outer-joined to a subquery

hi good people please find the code below is running in db2 but fails in oracle.
select * from
(select distinct e.time_stamp,e.applicationid, e.processname,
e.stage, e.initiatingsource, e.status, e.start_time, i.consultant,
g.cifnumber, g.applicantfirstname, g.applicantlastname,
case when e.branch is not null
then e.branch
else case when g.branch is not null
then g.branch
else i.branch
end
end as branch
from (select c.time_stamp, b.applicationid, b.processname,
b.stage, b.branch, b.initiatingsource,
case when d.status is null
then c.status
else d.status
end as status,
c.time_stamp as START_TIME,
case when d.time_stamp is not null
then d.time_stamp
else current_timestamp
ens as END_TIME
from (select distinct f.applicationid, f.branch, f.initiatingsource, f.processname,
case when f.stage in ('START''END')
then 'APPLICATION'
else f.stage, f.stagecounter
from processmetric f) b
left join processmetric c on b.applicationid = c.applicationid and b.processname = c.processname and (b.stage = c.stage or (b.stage = 'APPLICATION' and c.stage = 'START')) and b.stagecounter = c.stagecounter and c.phase = 'START'
left join processmetric d on b.applicationid = d.applicationid and b.processname = d.processname and (b.stage = d.stage or (b.stage = 'APPLICATION' and d.stage = 'END')) and b.stagecounter = d.stagecounter and d.phase ='END')e
left join applicationcustomerdata g on g.applicationid = e.applicationid
and g.time_stamp in (select max(x.time_stamp)
from applicationcustomerdata x
where x.applicationid = g.applicationid
)
left join applicationdata i on i.applicationid = e.applicationid
and i.time_stamp in (select max(z.time_stamp)
from applicationdata z
where z.applicationid = i.applicationid
)
order by e.start_time
) a
where a.start_time is not null and a.stage not in ('APPLICATION') and a.status not in ('COMPLETE' , 'COMPLETED' , 'CANCEL', 'FRAUD' , 'DECLINE') and a.stage = 'VERIFICATION';
Oracle don't allow to make outer join with subquery. Following 2 joins are problematic ones:
left join applicationcustomerdata g on g.applicationid = e.applicationid
and g.time_stamp in (select max(x.time_stamp)
from applicationcustomerdata x
where x.applicationid = g.applicationid
)
left join applicationdata i on i.applicationid = e.applicationid
and i.time_stamp in (select max(z.time_stamp)
from applicationdata z
where z.applicationid = i.applicationid
)
You need to rewrite statement (if you need this all in one SQL) or write some PL/SQL loops through data.