Solved

How to get max date

Posted on 2010-11-11
8
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 skipping a few tables 7 58
Sql query 107 88
Query to return total 6 25
LAG_ROWID - how do I get the right order using this query? 2 22
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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