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
Solved

MySQL Max of 2 queries in a Union

Posted on 2014-09-04
3
628 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 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

792 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