Solved

help wih SQL

Posted on 2014-01-17
6
267 Views
Last Modified: 2014-01-17
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?
0
Comment
Question by:HLRosenberger
  • 4
6 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
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
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
SELECT TOP 1 id, due_date
FROM your_table
ORDER BY due_date DESC
0
 
LVL 1

Author Comment

by:HLRosenberger
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:HLRosenberger
Comment Utility
This will be a view, so order by does not help.
0
 
LVL 1

Author Comment

by:HLRosenberger
Comment Utility
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
 
LVL 1

Author Closing Comment

by:HLRosenberger
Comment Utility
Thaanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now