How to get the Max(date) from a table

I have 2 tables:

Upload_Config
Config_Key (PK)
File_Title

Upload_History
Config_Key (FK)
Uploaded_Date_Time

The same files get uploaded a couple of times a day and I am writing a stored procedure to display the  latest update:

Example:
Config_Key        File_Title          Uploaded_Date_Time
20                       Sun.xls            2008-11-12 14:44:24.630
20                       Sun.xls            2008-11-13 13:06:24.627

My question is how can I display the second row only?

JessyRobinson1234Asked:
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.

Kevin CrossChief Technology OfficerCommented:
For SQL 2005, try something like this:
;WITH uploads AS (
SELECT c.*, h.Uploaded_Date_Time
, row_number() OVER (PARTITION BY c.Config_Key ORDER BY h.Uploaded_Date_Time DESC) AS Row_Number
FROM Upload_Config c
INNER JOIN Upload_History h ON c.Config_Key = h.Config_Key
)
SELECT Config_key
, File_Title
, Upload_Date_Time
FROM uploads
WHERE Row_Number = 1
ORDER BY Config_Key

Open in new window

0
Kevin CrossChief Technology OfficerCommented:
You could also do like this:
(forgive my type-o above as well as Upload_Date_time should be Uploaded_Date_Time)
SELECT c.*, h.Uploaded_Date_Time
FROM Upload_Config c
INNER JOIN (SELECT Config_Key, MAX(Uploaded_Date_Time) AS Uploaded_Date_Time FROM Upload_History GROUP BY Config_Key) h ON c.Config_Key = h.Config_Key

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
97WideGlideCommented:
Wouldn't this do it ?

Select upload_config.config_key, file_title, max(uploaded_date_time)
from
  upload_config, upload_history
where
  upload_config.config_key = upload_history.config_key
group by config_key;
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

JessyRobinson1234Author Commented:
Perfect, thanks a lot!
0
Kevin CrossChief Technology OfficerCommented:
97WIdeGlide, that would work in this case if you also group by the file_title.  I chose to not use that method so this can fit cases where there are a lot more columns involved that two. :)

I would use the ANSI standard for joining though instead of cartesian join as you have it.
0
97WideGlideCommented:
mwvisa1 - thank you very much for commenting on my solution.   I am learning also...  :)
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
Microsoft SQL Server 2005

From novice to tech pro — start learning today.