如果 where 条件位于连接表上,我会得到重复的结果吗?

发布于 2024-12-06 02:11:13 字数 732 浏览 5 评论 0原文

我有三个表,一个是任务,是工厂工人分批完成的任务,另一个表,每个任务有一个batch_id,多个任务可以有相同的batch_id。第三个表是batch_log,其中某人处理批次的所有时间都以唯一id、batch_id、该人的用户id、start_time 和end_time 的形式记录。

每个任务都有一个以秒为单位的estimated_recurring 时间和一个以秒为单位的estimated_nonrecurring 时间。我希望获得按操作 ID 分组的这两个字段的总和,操作 ID 是任务表中的一个字段,用于表示是否是激光切割、去毛刺、喷漆等任务。

问题是我只想要在某个时间段内批次日志中包含批次的任务。批次在batch_log 表中具有多个条目。

这是我正在尝试的:

SELECT 
  operation_id, SUM(t.estimated_nonrecurring + t.estimated_recurring) / 3600 as work_completed
FROM tasks t
INNER JOIN batch_log l on t.batch_id = l.batch_id
WHERE 
  l.start_time BETWEEN DATE("10:00:00") AND DATE(NOW())
AND
  l.time_elapsed < "10:00:00"
GROUP BY t.operation_id

我担心我会得到比实际更高的估计,因为任务表中的多个条目可以具有相同的批次。

I have three tables, one is tasks which are tasks that factory workers complete by putting them into batches, which is another table, each task has a batch_id, multiple tasks can have the same batch_id. The third table is the batch_log where all time that someone has worked on a batch is recorded in the form of a unique id, a batch_id, the person's userid, the start_time and the end_time.

Each task has an estimated_recurring time in seconds and an estimated_nonrecurring time in seconds. I'm looking to get the sum of both of those fields grouped by the operation_id which is a field in the tasks table to represent whether it is a laser cutting, deburr, painting, etc task.

The issue is that I only want the tasks that have batches that were in the batch_log during a time_period. Batches have multiple entries in the batch_log table.

Here is what I'm trying:

SELECT 
  operation_id, SUM(t.estimated_nonrecurring + t.estimated_recurring) / 3600 as work_completed
FROM tasks t
INNER JOIN batch_log l on t.batch_id = l.batch_id
WHERE 
  l.start_time BETWEEN DATE("10:00:00") AND DATE(NOW())
AND
  l.time_elapsed < "10:00:00"
GROUP BY t.operation_id

I'm concerned that I will get a higher estimate than is real because multiple entries in the task table can have the same batch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫弦 2024-12-13 02:11:13

因为您只需要基于批处理日志文件中的任务的时间,所以您发布的内容肯定不起作用。

假设您有 3 个表:Tasks、Batches 和 BatchLog。

你会想要这个:

SELECT
      SUM(t.estimated_nonrecurring + t.estimated_recurring) / 3600 as work_completed,
      t.OperationID
FROM
      Tasks t
INNER JOIN
       Batches b
ON
       b.BatchID = t.BatchID
INNER JOIN
       BatchLog bl
ON
       bl.BatchID = b.BatchID
WHERE  
  bl.start_time BETWEEN DATE("2011-08-01") AND DATE(NOW())
GROUP BY
       t.OperationID

Because you only want the times based on tasks that were in your batch log file what you posted will definately not work.

Assuming you have 3 tables: Tasks, Batches, and BatchLog.

You'll want this:

SELECT
      SUM(t.estimated_nonrecurring + t.estimated_recurring) / 3600 as work_completed,
      t.OperationID
FROM
      Tasks t
INNER JOIN
       Batches b
ON
       b.BatchID = t.BatchID
INNER JOIN
       BatchLog bl
ON
       bl.BatchID = b.BatchID
WHERE  
  bl.start_time BETWEEN DATE("2011-08-01") AND DATE(NOW())
GROUP BY
       t.OperationID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文