Solved

help wih SQL

Posted on 2014-01-17
6
270 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
ID: 39789470
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
ID: 39789500
SELECT TOP 1 id, due_date
FROM your_table
ORDER BY due_date DESC
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 39789504
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 1

Author Comment

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

Author Comment

by:HLRosenberger
ID: 39789535
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
ID: 39789565
Thaanks
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Backup Question 2 29
tools to scan a SQL server's problem 14 26
Parse this column 6 23
Need help in debugging a UDF results 7 15
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

791 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