Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL query

Posted on 2014-04-27
2
Medium Priority
?
480 Views
Last Modified: 2014-04-27
I need to add a column to this query...

SELECT
       CONCAT(s.SCCODE," ",s.SCC_DESC) 'Desc',
       t.UM 'UoM',
       c.POPU 'Orig Qty',
       IF(t.UM = 'LS', 1, ROUND(c.POUEST, 0)) 'Orig PR',
       FORMAT(((c.POPU / c.POUEST) * c.POEST), 0) 'Est Cost',
       FORMAT(SUM(m.PLACEMENT), 0) 'Placed JTD'
FROM materials m
JOIN `release` r ON m.RELEASE_ID = r.RELEASE_ID
JOIN ccode c ON r.CCODE_ID = c.CCODE_ID
JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
JOIN sctype t ON s.SCTYPE = t.SCTYPE_ID
JOIN job j ON m.JOB_ID = j.JOB_ID
WHERE j.JOB_ID = 7398
AND m.DATE_PLACE <= '2014-04-20'
AND m.DELETED = 'N'
AND c.POPU > 0
GROUP BY SUBSTRING(s.SCCODE,1,5)
ORDER BY s.SCTYPE, s.SCCODE;

The column I'm adding is the following query...

SELECT SUM(l.HOURS) Hours_JTD
FROM labor l
INNER JOIN ccode c ON c.CCODE_ID = l.CCODE_ID
INNER JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
INNER JOIN job j ON j.JOB_ID = l.JOB_ID
WHERE l.JOB_ID = 7398
AND l.DATE_WORK <= '2014-04-20'
AND l.DELETED = 'N'
GROUP BY SUBSTRING(s.SCCODE,1,5);

The output from this query is four rows:
640
182.5
1924.5
986

When I try a Cross Join as below the last column the first query (Placed JTD) radically changes and the last column incorrectly returns:
640
640
640
640

SELECT
       CONCAT(s.SCCODE," ",s.SCC_DESC) 'Desc',
       t.UM 'UoM',
       c.POPU 'Orig Qty',
       IF(t.UM = 'LS', 1, ROUND(c.POUEST, 0)) 'Orig PR',
       FORMAT(((c.POPU / c.POUEST) * c.POEST), 0) 'Est Cost',
       FORMAT(SUM(m.PLACEMENT), 0) 'Placed JTD',
       l.Hours_JTD
FROM materials m
JOIN `release` r ON m.RELEASE_ID = r.RELEASE_ID
JOIN ccode c ON r.CCODE_ID = c.CCODE_ID
JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
JOIN sctype t ON s.SCTYPE = t.SCTYPE_ID
JOIN job j ON m.JOB_ID = j.JOB_ID
CROSS JOIN (                                        /* l */
            SELECT SUM(l.HOURS) Hours_JTD
            FROM labor l
            INNER JOIN ccode c ON c.CCODE_ID = l.CCODE_ID
            INNER JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
            INNER JOIN job j ON j.JOB_ID = l.JOB_ID
                  WHERE l.JOB_ID = 7398
                  AND l.DATE_WORK <= '2014-04-20'
                  AND l.DELETED = 'N'
                  GROUP BY SUBSTRING(s.SCCODE,1,5)
           ) l
WHERE j.JOB_ID = 7398
AND m.DATE_PLACE <= '2014-04-20'
AND m.DELETED = 'N'
AND c.POPU > 0
GROUP BY SUBSTRING(s.SCCODE,1,5)
ORDER BY s.SCTYPE, s.SCCODE;

I've attached a mysql dump with data.
jds-s-0427.sql
0
Comment
Question by:hdcowboyaz
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 40026323
The additional query must be run once-per-row based on the results of the first query. If this actually makes sense, then it should be doable as a subquery like this I believe. Untested, but fingers crossed...

SELECT
       CONCAT(s.SCCODE," ",s.SCC_DESC) 'Desc',
       t.UM 'UoM',
       c.POPU 'Orig Qty',
       IF(t.UM = 'LS', 1, ROUND(c.POUEST, 0)) 'Orig PR',
       FORMAT(((c.POPU / c.POUEST) * c.POEST), 0) 'Est Cost',
       FORMAT(SUM(m.PLACEMENT), 0) 'Placed JTD',
(SELECT SUM(l.HOURS) Hours_JTD
FROM labor l
INNER JOIN ccode c ON c.CCODE_ID = l.CCODE_ID
INNER JOIN sccode s2 ON s2.SCCODE_ID = c.SCCODE_ID
INNER JOIN job j ON j.JOB_ID = l.JOB_ID
WHERE l.JOB_ID = 7398
AND l.DATE_WORK <= '2014-04-20'
AND l.DELETED = 'N'
AND s2.SCTYPE = s.SCTYPE
AND SUBSTRING(s2.SCCODE,1,5) = SUBSTRING(s.SCCODE,1,5)
) as Hours_JTD
FROM materials m
JOIN `release` r ON m.RELEASE_ID = r.RELEASE_ID
JOIN ccode c ON r.CCODE_ID = c.CCODE_ID
JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
JOIN sctype t ON s.SCTYPE = t.SCTYPE_ID
JOIN job j ON m.JOB_ID = j.JOB_ID
WHERE j.JOB_ID = 7398
AND m.DATE_PLACE <= '2014-04-20'
AND m.DELETED = 'N'
AND c.POPU > 0
GROUP BY SUBSTRING(s.SCCODE,1,5)
ORDER BY s.SCTYPE, s.SCCODE;

Open in new window


The subquery uses the alias s2 instead of s as we need to join to columns in s of the main query.

This probably isn't the most efficient form of the query, but hopefully it will achieve what you need. Otherwise, some other experts may have time to suggest some alternatives.
0
 

Author Closing Comment

by:hdcowboyaz
ID: 40026334
Perfect


Thanks
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

886 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