get the latest record

i have a selection like this.

 FullName, DateCreated, NumVal

How do I return the FullName where numval = 1000 and also get the one with the latest DateCreated?
vbnetcoderAsked:
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.

Rgonzo1971Commented:
Hi,

pls try
SELECT TOP 1 FullName, DateCreated, NumVal from tbl WHERE NumVal = 1000 ORDER BY DateCreated DESC

Open in new window

Regards

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
Éric MoreauSenior .Net ConsultantCommented:
or if you have multiple fullname for numval=1000:

select FullName, MAX(DateCreated) as DateCreated, NumVal
from YourTableName
where NumVal = 1000
Group By FullName, NumVal

Open in new window

PortletPaulEE Topic AdvisorCommented:
If it is important to get the complete row ("record") then using row_number() over() achieves this. I frequently use this approach if folks ask for "the latest" or similar:
SELECT
      *
FROM (
      SELECT
            *
          , ROW_NUMBER() OVER (ORDER BY DateCreated DESC) AS rn
      FROM yourtable) d
      WHERE numval = 1000
WHERE rn = 1

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mark WillsTopic AdvisorCommented:
agree with paul, row_number() works well...

I prefer the CTE - Common Table Expression - but much of a muchness

;with latest as
( select row_number() over (partition by numval order by datecreated desc) as rn,
       FullName, DateCreated, NumVal
  from yourtable
  where numval = 1000
) select FullName, DateCreated, NumVal
  from latest
  where rn = 1

Open in new window

awking00Information Technology SpecialistCommented:
>>How do I return the FullName where numval = 1000 and also get the one with the latest DateCreated?<<
 
Just to be clear. if you have data like this -
FULLNAME                 NUMVAL DATECREAT
-------------------- ---------- ---------
John Doe                    900 19-MAR-18
Mary Smith                 1000 09-MAR-18
Bill Jones                  800 28-MAR-18
Mary Smith                 1000 27-FEB-18

Would you only want to retrieve Mary Smith, 09-MAR-18 as the one with a numval of 1000 and the latest date with that numval, or would you also want to retrieve Bill Jones because it has the absolute latest date?
vbnetcoderAuthor Commented:
ty
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
SQL

From novice to tech pro — start learning today.