help with query - selecting only the most recent row by a date field

i have a join with about ten tables.   The following query works except i need one more filter or select statement to get this to work and i am unable to at this time.
in the table billing.sendbillto  there is some data that is causing duplicates.   it has a column called effectivedate.   I need this query to return only the rows where the effective date is most current.     ie... an account might have 3 or 4 sendbill to addresses.   the effective date for the application tells the system which mailing address to use.  I need this query to only pull one row per account and i need it to pull where i.effective date is the most recent date.

any thoughts

select b.accountnumber ,g.RESPONSIBLEPARTY ,i.addr1, i.addr2, i.city, i.state, i.zip, h.stno, h.stname, h.city AS City1, h.zip as zip1, f.dayphn,
e.fedtaxid, e.DRIVERLICENSENO, e.dob,a.delinquencyentrydate, sum(j.tranamt) as Balance_Owed, (select max(TRANDATE) from billing.ACCTTRAN z where z.ACCOUNTKEY = a.ACCOUNTKEY and TRANSACTIONDESIGNATOR = 10 and TRANSACTIONTYPE  = 9)[lastpayment] , 'ACS', i.effectivedate
from Billing.ACCOUNTDELINQUENCY a
join billing.account b on a.accountkey = b.accountkey
join billing.delinquencymilestone c on a.MILESTONEKEY = c.MILESTONEKEY
join billing.acctcntc d on a.accountkey = d.accountkey
join resources.cntctid e on d.idkey = e.idkey
join resources.contact f on d.cntctkey = f.cntctkey
join billing.responsibleparty g on a.accountkey = g.accountkey
join property.address h on b.addrkey = h.addrkey
join billing.sendbillto i on a.accountkey = i.accountkey
join billing.accttran j on b.accountkey = j.accountkey
join billing.RESPONSIBLEPARTYLOG k on e.idkey = k.idkey
where a.DELINQUENCYLEVEL = '5'
group by b.accountnumber, g.RESPONSIBLEPARTY,i.addr1, i.addr2, i.city, i.state, i.zip,h.stno, h.stname, h.city, h.zip, f.dayphn, e.FEDTAXID, e.DRIVERLICENSENO, e.dob, a.DELINQUENCYENTRYdate, a.ACCOUNTKEY, i.EFFECTIVEDATE
jamesmetcalf74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
You need to use a MAX function on that particular datetime column to get the top or latest row id then retrieve rest of the data based on that matching id
0
Scott PletcherSenior DBACommented:
instead of this:
join billing.sendbillto i on a.accountkey = i.accountkey
do this:
cross apply (
    select top (1) i2.*
    from billing.sendbillto i2
    where a.accountkey = i2.accountkey
    order by i2.effectivedate desc
) as i
or this:
join (
    select *, row_number() over(partition by accountkey order by effectivedate desc) as row_num
    from billing.sendbillto
) as i on i.accountkey = a.accountkey and i.row_num = 1


Depending on the table indexing, either could perform better.  For example, if the table is indexed on ( accountkey, effectivedate ), the first query should perform (much) better.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
Do you really need to GROUP BY all those columns, or are you having to do that just because of the sum()?  If just the sum(), then do this instead:

select b.accountnumber ,g.RESPONSIBLEPARTY ,i.addr1, i.addr2, i.city, i.state, i.zip, h.stno, h.stname, h.city AS City1, h.zip as zip1, f.dayphn,
e.fedtaxid, e.DRIVERLICENSENO, e.dob,a.delinquencyentrydate, j.tranamt as Balance_Owed, (select max(TRANDATE) from billing.ACCTTRAN z where z.ACCOUNTKEY = a.ACCOUNTKEY and TRANSACTIONDESIGNATOR = 10 and TRANSACTIONTYPE  = 9)[lastpayment] , 'ACS', i.effectivedate
from Billing.ACCOUNTDELINQUENCY a
join billing.account b on a.accountkey = b.accountkey
join billing.delinquencymilestone c on a.MILESTONEKEY = c.MILESTONEKEY
join billing.acctcntc d on a.accountkey = d.accountkey
join resources.cntctid e on d.idkey = e.idkey
join resources.contact f on d.cntctkey = f.cntctkey
join billing.responsibleparty g on a.accountkey = g.accountkey
join property.address h on b.addrkey = h.addrkey
join (
    select *, row_number() over(partition by accountkey order by effectivedate desc) as row_num
    from billing.sendbillto
) as i on i.accountkey = a.accountkey and i.row_num = 1
join (
    select accountkey, sum(tranamt) as tranamt
    from billing.accttran
    group by accountkey
) as j on j.accountkey = b.accountkey
join billing.RESPONSIBLEPARTYLOG k on e.idkey = k.idkey
where a.DELINQUENCYLEVEL = '5'
1
jamesmetcalf74Author Commented:
Thanks Scott!
worked like a charm
0
Scott PletcherSenior DBACommented:
Great!  Thanks for the feedback.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.