?
Solved

MAX(Time) in MySQL

Posted on 2014-11-10
8
Medium Priority
?
226 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 66

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 66

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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this article, we’ll look at how to deploy ProxySQL.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

771 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