Solved

MAX(Time) in MySQL

Posted on 2014-11-10
8
221 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
 
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now