briangriffin
Programmer
This query takes a little over a minute to run, but I think it should run faster. I'm not too skilled at interpreting execution plans, but I don't see any red flags - there is a sort for 37%, but I don't know where that is coming from. When I run each component individually they all take less than a couple of seconds.
Any advice is always appreciated.
Any advice is always appreciated.
Code:
declare @v_latestmergedate smalldatetime
set @v_latestmergedate = (select max(mergedate) from ekw1.rml_opi.dbo.OPI_Account_Collections)
select
a.accountnumber,
a.companyid,
replace(fullname,'~','') as FullName,
city,
state,
a.unbilled + a.thirty + a.sixty + a.ninety + a.overninety as priorbalance,
b.unbilled,
b.thirty,
b.sixty,
b.ninety,
b.over90,
b.inbaddebt,
b.currentbalance,
b.lastnotedate,
b.lastnote,
b.lastpaymentdate,
b.lastpayment,
b.paymentplannote,
b.lastPPPayment,
b.PPBalance,
a.LetterSent,
case when (b.lastnotedate > @v_latestmergedate)
or (b.lastpaymentdate > @v_latestmergedate)
or (b.currentbalance <> a.unbilled + a.thirty + a.sixty + a.ninety + a.overninety) then '***' else '' end as AccountChanged
from ekw1.rml_opi.dbo.OPI_Account_Collections a
left outer join
(
select
a.accountnumber,
a.companyID,
sum(case when b.firststatementdate is null then b.amount else 0 end) as 'Unbilled',
sum(case when datediff(dd,b.firststatementdate,getdate()) <= 30 then b.amount else 0 end) as 'Thirty',
sum(case when datediff(dd,b.firststatementdate,getdate()) between 31 and 60 then b.amount else 0 end) as 'Sixty',
sum(case when datediff(dd,b.firststatementdate,getdate()) between 61 and 90 then b.amount else 0 end) as 'Ninety',
sum(case when datediff(dd,b.firststatementdate,getdate()) > 90 then b.amount else 0 end) as 'Over90',
sum(case when b.baddebtflag = 1 then b.amount else 0 end) as 'InBadDebt',
(sum(case when b.firststatementdate is null then b.amount else 0 end) +
sum(case when datediff(dd,b.firststatementdate,getdate()) <= 30 then b.amount else 0 end) +
sum(case when datediff(dd,b.firststatementdate,getdate()) between 31 and 60 then b.amount else 0 end) +
sum(case when datediff(dd,b.firststatementdate,getdate()) between 61 and 90 then b.amount else 0 end) +
sum(case when datediff(dd,b.firststatementdate,getdate()) > 90 then b.amount else 0 end) -
sum(case when b.baddebtflag = 1 then b.amount else 0 end)) as CurrentBalance,
addnotes.changeddt as LastNoteDate,
addnotes.note as LastNote,
addpmt.postdate as LastPaymentDate,
addpmt.amount as LastPayment,
ppqry.currentbalance as PPBalance,
ppqry.ppnote as PaymentPlanNote,
ppqry.lastpaymentdate as LastPPPayment
from dbo.OPI_Account_Collections a left outer join rmlbilling1.ois.dbo.vw_am_GetAccount_InAR b with(nolock)
on a.accountid = b.accountid
and a.companyid = b.companyid
-------------------------------
left outer join
(
select
x.accountid,
x.companyid,
x.changeddt,
x.note
from rmlbilling1.ois.dbo.tblaccount_notes x with(nolock) inner join
(select
accountid,
companyid,
max(id) as maxid,
max(changeddt) as maxdate
from rmlbilling1.ois.dbo.tblaccount_notes notes with(nolock)
where exists (select 1 from ekw1.RML_OPI.dbo.OPI_Account_Collections where mergedate = @v_latestmergedate
and accountid = notes.accountid
and companyid = notes.companyid )
group by accountid, companyid) noteqry
on x.accountid = noteqry.accountid
and x.id = noteqry.maxid) addnotes
on a.accountid = addnotes.accountid
and a.companyid = addnotes.companyid
-------------------------------
left outer join
(select
x.accountid,
x.companyid,
x.postdate,
x.amount
from rmlbilling1.ois.dbo.tblaccount_payment x with(nolock) inner join
(select
accountid,
companyid,
max(id) as maxid,
max(postdate) as maxdate
from rmlbilling1.ois.dbo.tblaccount_payment payment with(nolock)
where exists (select 1 from ekw1.RML_OPI.dbo.OPI_Account_Collections where mergedate = @v_latestmergedate
and accountid = payment.accountid
and companyid = payment.companyid )
group by accountid, companyid ) noteqry
on x.accountid = noteqry.accountid
and x.id = noteqry.maxid) addpmt
on a.accountid = addpmt.accountid
and a.companyid = addpmt.companyid
-------------------------------
left outer join
(select
accountid,
companyid,
sum(currentbalance) as currentbalance,
ppnote,
lastpaymentdate,
lastpayment as lastpppayment
from
rmlbilling1.ois.dbo.vw_am_Get_PaymentPlan PP with(nolock)
where currentbalance > 0
and exists (select 1 from ekw1.RML_OPI.dbo.OPI_Account_Collections where mergedate = @v_latestmergedate
and accountid = PP.accountid
and companyid = PP.companyid )
group by
accountid,
companyid,
ppnote,
lastpaymentdate,
lastpayment) ppqry
on a.accountid = ppqry.accountid
and a.companyid = ppqry.companyid
---------------------------------
group by
a.companyid,
addnotes.changeddt,
addnotes.note,
addpmt.postdate,
addpmt.amount,
ppqry.currentbalance,
ppqry.ppnote,
ppqry.lastpaymentdate,
a.accountnumber,
a.baddebtflag
) b
on a.accountnumber = b.accountnumber
and a.companyid = b.companyid
where mergedate = @v_latestmergedate