Solved

MySQL Max of 2 queries in a Union

Posted on 2014-09-04
3
623 Views
Last Modified: 2014-09-04
I have a MySQL query with 2 statements in a union - I want to get the MAX of both queries off of MaxOfStartTime giving 1 entry for each tool instead of for both. So if query 1 has the maxtime it shows query 1's result and if query 2 has the maxtime it shows query 2's result

(
SELECT SerializedAssets.SerialNumber, 
Max(JobDownholeEquipment.JobID) AS LastOfJobID, 
Max(JobDownholeEquipment.StartTime) AS MaxOfStartTime, 
'Job' AS JobType, Job.Company,Job.Rig,Job.RigNumber,Job.WellName,Job.City,Job.State
FROM SerializedAssets 
INNER JOIN JobDownholeEquipment ON SerializedAssets.SerialNumber = JobDownholeEquipment.ToolNumber
INNER JOIN Job ON Job.JobID = JobDownholeEquipment.JobID
GROUP BY SerializedAssets.SerialNumber
)
UNION
(

SELECT ToolAssemblies.ToolNumber, MAX(ToolAssemblies.Location) AS LastOfLocation, Max(ToolAssemblies.UploadTime) AS MaxOfUploadTime, 'Assembly' AS JobType,
"" AS Company,"" AS Rig,"" AS RigNumber,"" AS WellName,"" AS Cist,"" AS State
FROM ToolAssemblies
GROUP BY ToolAssemblies.ToolNumber
)

Open in new window

0
Comment
Question by:r3nder
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40303737
I think I would create a view from your query, then apply the syntax suggestion for MySQL from this article I wrote: http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
hope this helps
0
 
LVL 6

Author Comment

by:r3nder
ID: 40303767
ok how - I don't really have time to learn right now - I just need to figure  it out. In this particular scenario I have 1 record from each select statement that are the same tool number.
so the data looks like this
---------------------------------
1  | 12/12/2014  | Dallas
-----------------------------------
1  | 01/02/2009  | On job

and so on......
I need the max date of those records so that is displayed
-----------------------------------
1  | 12/12/2014  | Dallas
-----------------------------------
2  | 12/24/2014  | On Job

And so on
0
 
LVL 6

Author Comment

by:r3nder
ID: 40303930
Fixed
 query += " DROP VIEW if exists v; ";
                query += " Create View v as ( ";
                query += " SELECT SerializedAssets.SerialNumber AS ToolNumber,  ";
                query += " Max(JobDownholeEquipment.JobID) AS LastOfLocation,  ";
                query += " Max(JobDownholeEquipment.StartTime) AS MaxOfUploadTime,  ";
                query += " 'Job' AS JobType, Job.Company,Job.Rig,Job.RigNumber,Job.WellName,Job.City,Job.State ";
                query += " FROM SerializedAssets  ";
                query += " INNER JOIN JobDownholeEquipment ON SerializedAssets.SerialNumber = JobDownholeEquipment.ToolNumber ";
                query += " INNER JOIN Job ON Job.JobID = JobDownholeEquipment.JobID ";
                query += " GROUP BY SerializedAssets.SerialNumber ";
                query += " ) ";
                query += " UNION ALL ";
                query += " ( ";
                query += " SELECT ToolAssemblies.ToolNumber AS ToolNumber, MAX(ToolAssemblies.Location) AS LastOfLocation, Max(ToolAssemblies.UploadTime) AS MaxOfUploadTime, 'Assembly' AS JobType, ";
                query += " '' AS Company,'' AS Rig,'' AS RigNumber,'' AS WellName,'' AS City,'' AS State ";
                query += " FROM ToolAssemblies ";
                query += " GROUP BY ToolAssemblies.ToolNumber, 'Assembly' ";
                query += " ) ";
                query += " Order by ToolNumber; ";
                query += " Select * From v Group by ToolNumber ";

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now