Solved

MAX(Time) in MySQL

Posted on 2014-11-10
8
222 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Designer 19 40
insert wont work in SQL 14 21
SQL View nearest date 5 36
SQL Server Import/Error Wizard error 12 17
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
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

776 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