Solved

How to get max date

Posted on 2010-11-11
8
296 Views
Last Modified: 2012-05-10
I have a table which contains the times documents were created. There are two groups of documents, one that is created by our system automatically at 11:23 PM on a daily basis, and documents that were created by users. What I need to find out, is when the last user-created document was entered in the system in a 1 month range. For example... the date column looks like this:

date
2010-11-10 17:10
2010-11-10 17:23
2010-11-10 23:00
2010-11-11 17:24
2010-11-11 17:35
2010-11-11 23:00

My query would return the following results:
2010-11-10 17:23
2010-11-11 17:35

How can this be accomplished?
0
Comment
Question by:horalia
  • 4
  • 2
  • 2
8 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 34115265
I would suggest:SELECT TOP 1 DateField FROM YourTable WHERE DateField BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD' ORDER BY DateField DESCreplacing YYYY-MM-DD with the appropriate start and end dates for the date range you want.(I always get ASC and DESC mixed up... if that returns the OLDEST in the range, then use ASC instead of DESC at the end)
0
 

Author Comment

by:horalia
ID: 34115282
This will only return one row... I need the maximum date for a group of dates.....
0
 

Author Comment

by:horalia
ID: 34115304
I have created this query:
select convert(nvarchar(10),sentdatetime,110), max(sentdatetime)
from alg_fax (nolock)
where sentdatetime > '2010-11-01'
group by convert(nvarchar(10),sentdatetime,110)

But I get rows for 11:00 PM. How can I exclude these rows?
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34115307
This is going to be interesting.
What other columns are available on the table? That differentiates between user and system?

date
2010-11-10 17:10
2010-11-10 17:23  <<< why this?
2010-11-10 23:00
2010-11-11 17:24
2010-11-11 17:35  <<< why this?
2010-11-11 23:00
2010-11-11 23:55  <<< what if there is this one?

My query would return the following results:
2010-11-10 17:23
2010-11-11 17:35

My guess is that you want the latest datetime prior to 23:00, PER day?  See 3rd << note above.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34115313
select convert(nvarchar(10),sentdatetime,110), max(sentdatetime)
from alg_fax (nolock)
where sentdatetime > '2010-11-01' and datepart(hour, sentdatetime) < 23
group by convert(nvarchar(10),sentdatetime,110)
0
 

Author Comment

by:horalia
ID: 34115316
I want the times that show before 23:00. Maybe using DATEPART?
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 34115402
didn't register you were looking for a grouping...

Without knowing field names, my answer could have worked for your question as quoted below.
"What I need to find out, is when the last user-created document was entered in the system in a 1 month range"
0
 

Author Comment

by:horalia
ID: 34115419
I might have chosen the wrong comment, I want to award points and accept cyberkiwi's answer.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Select * from 6 41
Help with SQL joins 9 49
Bubble user-defined Sql RAISERROR(...) to c# exception 14 169
why sql server only update some statistics in the database ? 3 24
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

832 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