gpt4 book ai didi

mysql - 如何获得 SUM 以通过连接正确计算?

转载 作者:IT老高 更新时间:2023-10-28 23:53:12 24 4
gpt4 key购买 nike

所以我试图计算零件的数量、任务的数量、每项工作的数量以及制造每项工作所花费的时间,但我得到了一些奇怪的结果。如果我运行这个:

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

我现在得到了准确的零件和任务编号,但 time_elapsed 总和不正确。问题可能是什么?

当我尝试使用 distinct 时,我得到了一个非常低的数字(当我在寻找接近 10,000 的东西时,我得到了一个 1 到 30 之间的数字。)

更新:这里是创建代码:

http://pastebin.com/nbhU9rYh

http://pastebin.com/tdmAkNr4

http://pastebin.com/0TFCUaeQ

http://pastebin.com/fugr8C9U

http://pastebin.com/Zq0bKG2L

http://pastebin.com/k5rESUrq

关系是这样的:

  • mf_job 信息与工作相关联
  • 工作有部分
  • 零件有任务
  • 任务分批进行
  • batch_log 是一个包含批处理任务的所有开始和停止的表,它有一个 start_time 和一个 stop_time 以及一个 time_elapsed。

我正在尝试从每个 mf_job 的 batch_log 中获取所有 time_elapsed,其中一个字段中带有单词 backblaze 以及零件、任务和组件的数量。这一切都需要按 job.id 或 mf_job.id 分组

最佳答案

尝试将查询重写为:

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

关于mysql - 如何获得 SUM 以通过连接正确计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7233617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com