Solved

How to get max date

Posted on 2010-11-11
8
294 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
 
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

24 Experts available now in Live!

Get 1:1 Help Now