Solved

SQL Select Statement with MAX

Posted on 2012-04-04
6
307 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 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 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