MSSQL result return with doublet result, need other way than distinct

Dear expert

Please help me with this MSSQL Query:

SELECT FirstName LastName DatetimeCancel
FROM [Prod].[System].[Approvement] aa 
join [Prod].[Contact].[Individual] ci on aa.PersonalIdentityNumberSender = ci.PersonalIdentityNumber join [Prod].[Contact].[Address] ca 
on ci.Id = ca.Id join [Prod].[Subscription].[Member] rsm on rsm.IdMember = ca.Id join [Prod].[Invoice].[Receiver] ser on ser.Id = ca.Id
where ApprovementStatusId = 7 and aa.DatetimeCancel is not null and aa.Id not in (select id from [Prod].[System].[Approvement] where ApprovementStatusId = 5 or ApprovementStatusId = 3 or ApprovementStatusId = 4 or ApprovementStatusId = 6 or ApprovementStatusId = 8 or ApprovementStatusId = 9 or ApprovementStatusId = 10 or ApprovementStatusId = 11 or ApprovementStatusId = 12 or ApprovementStatusId = 13 )

Open in new window


Result return with doublets because DatetimeCancel is diffirent, now I want only show the lastest time as result, in this case 2018-05-25 others ignore. Distinct can only random remove one (or more) doublets.
Is there a way to do this?
SC.PNG
LVL 1
WeTiAsked:
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.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Simple MAX Aggregate function would do..
SELECT FirstName, LastName, MAX(DatetimeCancel) DatetimeCancel
FROM [Prod].[System].[Approvement] aa 
join [Prod].[Contact].[Individual] ci on aa.PersonalIdentityNumberSender = ci.PersonalIdentityNumber join [Prod].[Contact].[Address] ca 
on ci.Id = ca.Id join [Prod].[Subscription].[Member] rsm on rsm.IdMember = ca.Id join [Prod].[Invoice].[Receiver] ser on ser.Id = ca.Id
where ApprovementStatusId = 7 and aa.DatetimeCancel is not null and aa.Id not in (select id from [Prod].[System].[Approvement] where ApprovementStatusId = 5 or ApprovementStatusId = 3 or ApprovementStatusId = 4 or ApprovementStatusId = 6 or ApprovementStatusId = 8 or ApprovementStatusId = 9 or ApprovementStatusId = 10 or ApprovementStatusId = 11 or ApprovementStatusId = 12 or ApprovementStatusId = 13 )
GROUP BY FirstName, LastName

Open in new window

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
WeTiAuthor Commented:
Yes Max and group by helped thanks alot
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Welcome, glad to assist!!
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
Query Syntax

From novice to tech pro — start learning today.