Solved

SQL Select Statement with MAX

Posted on 2012-04-04
6
298 Views
Last Modified: 2012-04-04
I need assistanace in combining this SQL Statement.
SQL A-

SELECT Transaction_DateTimestamp, Total_Hours, Date1, Employee_Name, Travel_Time from GrooveImports where Date1 >= '4/5/2012' and Date1 <= '4/8/2012' and Employee_Name = 'XYZ'

produces a list of employee dates and times with each record having a date timestamp of when the record was last modified. However, SQL A also retrieves ALL records between those dates, where I need to only return the MAX (DateTimestamp) of each day, so only the latest modified record is returned.

When I use the below statement for 1 day, this returns the latest record for that day.
So my question, is how can I return the latest record for each day when given a date range? (I am thinking it is a union, and/ or creating a temp table, but I am not the expert)


SELECT Transaction_DateTimestamp, Total_Hours, Date1, Employee_Name, Travel_Time from GrooveImports where Date1= '4/5/2012' AND  (GrooveImports.Transaction_DateTimeStamp =
                          (SELECT     MAX(Transaction_DateTimeStamp) AS Expr1
                            FROM          GrooveImports AS GrooveImports_1
                            WHERE      (Date1 = '4/4/2012')))
0
Comment
Question by:BKennedy2008
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
wshark83 earned 500 total points
ID: 37806161
how about:

SELECT a.Transaction_DateTimestamp, a.Total_Hours, a.Date1, a.Employee_Name, a.Travel_Time
from GrooveImports a
join  (SELECT     date1, MAX(Transaction_DateTimeStamp) AS Expr1
         FROM          GrooveImports
         Group by date1) b
on a.date1 = b.date1
and a.Transaction_DateTimeStamp = b.expr1
0
 

Author Comment

by:BKennedy2008
ID: 37806218
Will try
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37806228
Try this

SELECT max(Transaction_DateTimestamp) DateTimestamp, Total_Hours, Date1, Employee_Name, Travel_Time from GrooveImports where Date1 >= '4/5/2012' and Date1 <= '4/8/2012' and Employee_Name = 'XYZ'
Group by  Total_Hours, Date1, Employee_Name, Travel_Time

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:BKennedy2008
ID: 37806254
Sach- That produces nothing, I tried that one.

WSShark- yours almost does the trick, I just need to add somewhere the where:

where a.Date1 >= '4/5/2012' and a.Date1 <= '4/8/2012' and a.Employee_Name = 'xyz'
0
 

Author Comment

by:BKennedy2008
ID: 37806278
your the man WShark83, thanks for the quick response!


SELECT a.Transaction_DateTimestamp, a.Total_Hours, a.Date1, a.Employee_Name, a.Travel_Time
from GrooveImports a  
join  (SELECT     date1, MAX(Transaction_DateTimeStamp) AS Expr1
         FROM          GrooveImports
         Group by date1) b
on a.date1 = b.date1
and a.Transaction_DateTimeStamp = b.expr1
and a.employee_Name = 'xyz' and a.Date1 >='4/1/2012' and a.date1 <='4/6/2012'
0
 
LVL 6

Expert Comment

by:wshark83
ID: 37806291
nps glad to be able to help.....
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

758 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

19 Experts available now in Live!

Get 1:1 Help Now