[英]Aggregate functions with joins
因此,我试图计算零件的数量,任务的数量,每项工作的数量以及制造每项工作所花费的时间,但是我得到了一些时髦的结果。 如果我运行此命令:
SELECT
j.id,
mf.special_instructions,
count(p.id) as number_of_parts,
count(t.id) as number_of_tasks,
j.quantity as job_quantity
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
然后我得到其中一个记录:
"id";"special_instructions";"number_of_parts";"number_of_tasks";"job_quantity"
"10cfa05c-a8c0-b1e6-2a36-4e52579c82ab";"BACKBLAZE TEMPLATE full assembled/tested RAL 5017 custom Logo - cutout";"105";"105";"1"
意味着应该有105个任务和105个部分,这很不正常,因为一个部分通常有8到10个任务,而且一个工作通常每个工作中可能只有10个部分。 当我运行这个:
SELECT * from ramses.parts where job_id = "10cfa05c-a8c0-b1e6-2a36-4e52579c82ab"
仅返回13行。 我加入事物的方式是否存在某种问题?
使用count(distinct p.id)
计算不同零件的数量。 现在,您只是在计算总行数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.