So I'm trying to count the number of parts, number of tasks, the quantity in each job and the time that it took to manufacture each job but I'm getting some funky results. If I run this:
SELECT
j.id,
mf.special_instructions,
count(distinct p.id) as number_of_different_parts,
count(distinct t.id) as number_of_tasks,
SUM(distinct j.quantity) as number_of_assemblies,
SUM(l.time_elapsed) as time_elapsed
FROM
sugarcrm2.mf_job mf
INNER JOIN ramses.jobs j on
mf.id = j.mf_job_id
INNER JOIN ramses.parts p on
j.id = p.job_id
INNER JOIN ramses.tasks t on
p.id = t.part_id
INNER JOIN ramses.batch_log l on
t.batch_id = l.batch_id
WHERE
mf.job_description LIKE "%BACKBLAZE%" OR
mf.customer_name LIKE "%BACKBLAZE%" OR
mf.customer_ref LIKE "%BACKBLAZE%" OR
mf.technical_company_name LIKE "%BACKBLAZE%" OR
mf.description LIKE "%BACKBLAZE%" OR
mf.name LIKE "%BACKBLAZE%" OR
mf.enclosure_style LIKE "%BACKBLAZE%" OR
mf.special_instructions LIKE "%BACKBLAZE%"
Group by j.id
and I now get accurate parts and tasks numbers but the time_elapsed sum isn't correct. What could the problem be?
When I try it with distinct
I get a veeeeery low number (like something between 1 and 30 when I'm looking for something closer to 10,000.)
UPDATE: here is the create code:
http://pastebin.com/nbhU9rYh
http://pastebin.com/tdmAkNr4
http://pastebin.com/0TFCUaeQ
http://pastebin.com/fugr8C9U
http://pastebin.com/Zq0bKG2L
http://pastebin.com/k5rESUrq
The relationships are like this:
I am trying to get all of the time_elapsed from the batch_log for each mf_job with the word backblaze in one of it's fields along with the number of parts, tasks and assemblies. This all needs to be grouped by job.id or mf_job.id
Try and rewrite the query to:
SELECT
j.id,
mf.special_instructions,
count(p.id) as number_of_different_parts,
count(t.id) as number_of_tasks,
SUM(j.quantity) as number_of_assemblies,
SEC_TO_TIME(SUM(l.seconds_elapsed)) as time_elapsed
FROM
sugarcrm2.mf_job mf
INNER JOIN ramses.jobs j on
mf.id = j.mf_job_id
INNER JOIN ramses.parts p on
j.id = p.job_id
INNER JOIN ramses.tasks t on
p.id = t.part_id
INNER JOIN (
SELECT rl.batch_id
, SUM(TIME_TO_SEC(rl.time_elapsed)) as seconds_elapsed
FROM ramses.batch_log rl
GROUP BY rl.batch_id
) l ON (t.batch_id = l.batch_id)
WHERE
mf.job_description LIKE "%BACKBLAZE%" OR
mf.customer_name LIKE "%BACKBLAZE%" OR
mf.customer_ref LIKE "%BACKBLAZE%" OR
mf.technical_company_name LIKE "%BACKBLAZE%" OR
mf.description LIKE "%BACKBLAZE%" OR
mf.name LIKE "%BACKBLAZE%" OR
mf.enclosure_style LIKE "%BACKBLAZE%" OR
mf.special_instructions LIKE "%BACKBLAZE%"
GROUP BY j.id WITH ROLLUP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With