【问题标题】:How do I limit a selection with a where without removing all of the entries that meet the condition in the join?如何在不删除满足联接条件的所有条目的情况下使用 where 限制选择?
【发布时间】:2011-12-07 20:11:44
【问题描述】:

我们根据预计完成的工作量除以支付的小时数来计算奖金。我们的系统中有一个人不是我们支付的,我想将他从报告中删除。

在这个查询中,我得到的是该用户在每个批次上的所有跟踪时间除以该批次上跟踪的总时间,减去 [name]。

批次的任务包含estimated_nonrecurring 和estimated_recurring。 批次有 batch_logs,其中 time_elapsed 是批次实际工作的时间。

发生的事情不是选择所有批次,然后获取为该用户跟踪的所有时间,而不是与 [name] 未跟踪的总跟踪时间相比,它忽略了具有 [ name] 作为其中的一个条目。

这是我的比率选择语句。

SELECT user_time_by_batch.batch_id, userid, user_time_by_batch / total_time_by_batch as ratio FROM 
( 
SELECT SUM(time_elapsed) user_time_by_batch, userid, batch_id 
FROM batch_log 
inner join batches on batch_log.batch_id = batches.id 
Where start_time between (?) and (?) 
   and end_time is not null and time_elapsed BETWEEN \"00:00:00\" AND \"10:00:00\" 
   and batch_id not in (\"-1\", \"-2\", \"-3\", \"-4\", \"-5\", \"-6\", \"-7\", \"-8\", \"-9\", \"-10\", \"-11\", \"-12\", \"-13\", \"-14\", \"-15\", \"-16\", \"-17\") 
   and batches.operation_id = ? 
   and batch_log.userid = ? 
GROUP BY batch_id, userid 
)  user_time_by_batch 
INNER JOIN 
( 
SELECT SUM(time_elapsed) total_time_by_batch, batch_id 
FROM batch_log 
inner join batches on batch_log.batch_id = batches.id 
Where start_time between (?) and (?)  
  and end_time is not null and time_elapsed BETWEEN \"00:00:00\" AND \"10:00:00\" 
  and batch_id not in (\"-1\", \"-2\", \"-3\", \"-4\", \"-5\", \"-6\", \"-7\", \"-8\", \"-9\", \"-10\", \"-11\", \"-12\", \"-13\", \"-14\", \"-15\", \"-16\", \"-17\") 
  and batches.operation_id = ? 
  and batch_log.userid != 'name' 
GROUP BY batch_id 
) total_time_by_batch 
ON user_time_by_batch.batch_id = total_time_by_batch.batch_id ;

【问题讨论】:

  • 您说“它忽略了每一个以 smacpherson 作为条目的批次。”我认为这正是你告诉它要做的事情。我的猜测是,您需要按用户和批次(而不是单独批次)将时间相加,将麦克弗森排除在这些总和中,然后按批次将这些总和相加以获得您的批次总数。
  • 是的,我只是想把他排除在外:如果 bob 工作 6 小时而 mac 工作 3 小时,那么 bob 的比率应该是 1.0 而不是 6.66666
  • 我不确定,但我认为您现在正在做的是说“总结一下,但不要使用 mac 选择任何批次”。我认为您想要做的是将所有不是 mac 的用户相加,然后将他们的时间相加以创建您的批次总数以进行比较。那么 bob 是 1.0。但我可能离基地很远。
  • 我希望能够获得每个人与批次中所有人相比的所有总数,就好像 mac 不存在一样。所以,基本上,忽略 mac 所在的每个 batch_log,但不是每个批次。

标签: mysql sql join group-by


【解决方案1】:

我会考虑尝试的一件事是将 batch_log.userid != 'smacpherson' 移到 JOIN 语句而不是 WHERE 子句中。我不确定这是怎样的最佳实践,但我已经通过这种方式成功地消除了奇怪的数据问题。

所以:

INNER JOIN 
( 
SELECT SUM(time_elapsed) total_time_by_batch, batch_id 
FROM batch_log 
inner join batches on batch_log.batch_id = batches.id and batch_log.userid != 'smacpherson'
Where start_time between (?) and (?)  
  and end_time is not null and time_elapsed BETWEEN \"00:00:00\" AND \"10:00:00\" 
  and batch_id not in (\"-1\", \"-2\", \"-3\", \"-4\", \"-5\", \"-6\", \"-7\", \"-8\", \"-9\", \"-10\", \"-11\", \"-12\", \"-13\", \"-14\", \"-15\", \"-16\", \"-17\") 
  and batches.operation_id = ?  
GROUP BY batch_id 
) total_time_by_batch 

【讨论】:

  • 会试试这个。看起来这可能是我需要的。
  • 我想建议,但我不知道它是否可行。我没有 mySQL 和 Access 在我这样做的测试连接中抛出。有兴趣看看你是否成功。
  • 嗯.. 我不明白加入选择。
  • 这对你有帮助吗,还是你还有问题?
猜你喜欢
  • 2015-05-16
  • 2020-02-09
  • 1970-01-01
  • 2017-09-16
  • 2021-06-06
  • 2023-02-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
相关资源
最近更新 更多