help wih SQL

I have a table with a date column (due_date).  I want to select the record with the max date, and return that date and the primary key  (ID).   how can I do this?
LVL 1
HLRosenbergerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Jim HornConnect With a Mentor Microsoft SQL Server Developer, Architect, and AuthorCommented:
Couple of ways to pull this off, this looks decent..
SELECT id, due_date
FROM your_table
WHERE due_date IN (SELECT max_date FROM your_table) 

Open in new window

0
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT TOP 1 id, due_date
FROM your_table
ORDER BY due_date DESC
0
 
HLRosenbergerAuthor Commented:
See below.   I left out part of my question.  These records say are tasks associated with a job table.   This query picks off the max date out for all the records.  I'm actually creating a view, so I need a set of records that show the task ID and task Max(DueDate) for each job.

How could I modify this to get what I need?


 SELECT id, duedate
  FROM [tdsQA].[dbo].[tds_grant_funding_request_report]
  WHERE  duedate IN (SELECT max(duedate) FROM [tds_grant_funding_request_report])
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
HLRosenbergerAuthor Commented:
This will be a view, so order by does not help.
0
 
HLRosenbergerAuthor Commented:
Ah, think I got it:

  SELECT id, duedate
  FROM [tdsQA].[dbo].[tds_grant_funding_request_report]
  WHERE  duedate IN
   (SELECT max(duedate) FROM [tds_grant_funding_request_report] group by fk_funding_requestID)

where fk_funding_requestID is the "job" primary key, in effect.
0
 
HLRosenbergerAuthor Commented:
Thaanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.