SQL - Group and Get Top 1

I have a sql statement that returns the below data

AccTID     PKID    RTID            BeginDate                     EndDate  
123123      888999      1      2014-05-01 00:00:00      2014-05-31 23:59:00
123123      888999      1      2014-06-01 00:00:00      2014-06-30 23:59:00
123123      888999      1      2014-07-01 00:00:00      2014-07-31 23:59:00
123123      888999      1      2015-04-01 00:00:00      2015-04-30 23:59:00
123123      888999      3      2014-05-01 00:00:00      2014-05-31 23:59:00
123123      888999      3      2014-06-01 00:00:00      2014-06-30 23:59:00
123123      888999      3      2015-01-01 00:00:00      2015-01-31 23:59:00
123123      888999      3      2015-02-01 00:00:00      2015-02-28 23:59:00
123123      888999      3      2015-04-01 00:00:00      2015-04-30 23:59:00

The sql statement is as so.
Select DISTINCT t1.AccTID, t2.pkid, t6.rtid, t5.beginDate, t5.enddate
From table1 t1
inner join table2 t2 on f1.AccTID = f2.AccTID
inner join table3 t3 on f2.esid = f3.esid
inner join table4 t4 on e.crid = en.crid
inner join table5 t5 on c.crid = e.crid
inner join table6 t6 on t6.cbid = t5.cbid
where t1.id = 123123
and t2.acctid = 888999
and (t6.rtid = 1 or t6.rtid = 3)
order by f1.AccTID, f2.pkid, t6.rtid, t5.beginDate, t5.enddate desc

Open in new window


I need to get is one record.  The max end date sorted by 1 then 3.
LVL 2
CipherISAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
quick try:

select a.* from
(
Select t1.AccTID, t2.pkid, t6.rtid, t5.beginDate, t5.enddate,
row_number() over (partition by t1.AccTID, t2.pkid, t6.rtid order by t5.enddate desc, t5.beginDate desc) idx
From table1 t1
inner join table2 t2 on f1.AccTID = f2.AccTID
inner join table3 t3 on f2.esid = f3.esid
inner join table4 t4 on e.crid = en.crid
inner join table5 t5 on c.crid = e.crid
inner join table6 t6 on t6.cbid = t5.cbid
where t1.id = 123123
and t2.acctid = 888999
and (t6.rtid = 1 or t6.rtid = 3)
Group By t1.AccTID, t2.pkid, t6.rtid, t5.beginDate, t5.enddate
) a
where a.idx = 1
order by a.AccTID, a.pkid, a.rtid, a.beginDate, a.enddate desc

Open in new window

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
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 2008

From novice to tech pro — start learning today.