?
Solved

SQL Select Statement with MAX

Posted on 2012-04-04
6
Medium Priority
?
309 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
[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
  • 3
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
wshark83 earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

770 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