Solved

MySQL Max of 2 queries in a Union

Posted on 2014-09-04
3
633 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
[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
  • 2
3 Comments
 
LVL 143

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

627 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