Solved

SQL Select Statement with MAX

Posted on 2012-04-04
6
302 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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