We calculate bonuses based on estimated work completed divided by hours paid. There is a person in our system who isn’t paid by us and I want to remove him from the report.
In this query I am getting all of the tracked time on each batch for that user divided by the total time tracked on that batch, minus [name].
Batches have tasks which contain the estimated_nonrecurring and the estimated_recurring.
Batches have batch_logs which have the time_elapsed which is the time that the batch has been actually worked on.
What is happening is instead of selecting all the batches and then getting all the time that was tracked for that user in comparison to the total time tracked that wasn’t tracked by [name], it is ignoring every batch that has [name] as an entry in it.
Here is my select statement for ratios.
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 ;
One thing I would consider trying is moving the batch_log.userid != ‘smacpherson’ in to the JOIN statement instead of in the WHERE clause. I’m not sure how best-practicey this is, but I’ve had success eliminating weird data issues by juggling that way.
So: