Solved

MAX(Time) in MySQL

Posted on 2014-11-10
8
224 Views
Last Modified: 2014-11-24
I have a Query I am running but it doesn't seem to ge thte max(UploadTime) on every query. I looked in the data base and saw times greater than the time being returned Example:
ID            UploadTime          Tech                                tID
3899      11/10/2014 12:40      Tech1               1067      3      2      5/19/2014      10/30/2014      649.25 <-- returns this one
3901      11/10/2014 13:19      Tech2             1067      3      2      5/19/2014      11/10/2014      649.25
3902      11/10/2014 13:25      Tech4             1067      3      2      5/19/2014      11/10/2014      649.25


DROP TABLE IF EXISTS Temp_MyTools;
CREATE TABLE Temp_MyTools(
Select sa.ToolType, sa.SerialNumber, MAX(cd.UploadTime) , cd.DefaultLocationIndex
from SerialA as sa
LEFT JOIN Control as cd ON cd.SerialNumber = sa.SerialNumber
where sa.tooltype in('CO')
AND Dead = 0 
GROUP BY sa.SerialNumber);
Select ToolType, SerialNumber FROM Temp_MyTools WHERE DefaultLocationIndex = 0

Open in new window

0
Comment
Question by:r3nder
[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
8 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 40433618
Does column [Dead] come from table Control?
Do all 3 records have tooltype = 'CO'?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40433619
For starters, your subquery's GROUP BY clause is missing sa.ToolType and cd.cd.DefaultLocationIndex.
All columns not participating in an aggregate such as SUM, COUNT, or MAX have to be in the GROUP BY.  

If you'd like some more reading on GROUP BY I have an image and code-heavy tutorial out there called SQL Server GROUP BY Solutions
0
 
LVL 6

Author Comment

by:r3nder
ID: 40433658
Yes all are tooltype are 'CO' and I added sa. to Dead
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 6

Author Comment

by:r3nder
ID: 40433663
@jim sorry it only added everything to the result set - not what I am looking for - I am looking for the max date where defaultlocationIndex = 0 (eventually 1 then 2)
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40433683
Should be fixed anyways, as I'm surprised the code executes without throwing an error.

>I am looking for the max date where defaultlocationIndex = 0 (eventually 1 then 2)
Then defaultlocationIndex needs to be in the GROUP BY.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40438120
Your question is a little ambiguous given that your example shows 4 column names (with apparently 9 column values), none of which are tool_type, serial_number, defaultlocationindex, or dead as mentioned in your query. Perhaps you can post some sample data for the SerialA and Control tables along with their structure (i.e. column names and datatypes) and your expected results.
0
 
LVL 6

Accepted Solution

by:
r3nder earned 0 total points
ID: 40438156
What I ended up doing was that query then up dating the temp table with a date from  the exact same query except where   DefaultLocationIndex = 1 then doing a select on the temp table
select * from Temp_MyTools WHERE 1stdate > 2ndDate
and that worked - but for some reason the max  method did not work - I could have been doing it wrong
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40461724
Thanks anyway
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

733 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