Reputation: 4094
I have four tables:
How would I get each batch worked for each user, the total elapsed from each batch log for those batch ids and the total estimate from each task id for that batch?
EDIT:
TABLES:
task
id estimated_nonrecurring estimated_recurring batch_id
batch
id operation_id date_entered
batch_log
id userid batch_id time_elapsed
operation
id name
I'm thinking:
get each user;
get a list of distinct batch_ids that they worked on;
get a sum of all the time_elapsed from each batch_log for those batch id;
get all the non_recurring and the recurring for each task with each batch_id;
so that the result is like
userid, operation, batch_id, total_elapsed, total_estimate, date_entered
The reasoning for doing this is so that it can be possible to rate the users on how productive they are and use these queries in excel. I think I may have to go with two queries:
Upvotes: 0
Views: 141
Reputation: 1692
select distinct bl.userid , t.batchid from batch_log bl inner join task t on t.taskid = bl.taskid ;
select sum(bl.time_spent) , b.batchid from batch b left join task t on b.batchid = t.batchid inner join batch_log bl on bl.taskid = t.taskid;
select sum(t.estimate) , b.batchid from batch b left join task t on b.batchid = t.batchid ;
Why is it called batch_log but it is about taskes and time spent ?
Upvotes: 1
Reputation: 65157
Something like:
SELECT bl.UserId, b.name, t.estimate
FROM batch_log as bl
JOIN batches as b
ON b.id = bl.batch_id
JOIN task as t
ON t.id = b.task_id
WHERE bl.UserId = 123
Hard to say without any sort of table structure to go by.
Upvotes: 1
Reputation: 7150
I'm not sure on the structure of your tables, but something like this should work:
select batch.id,
(select sum(batch.time)
from batch_log
inner join task on task.id = batch_log.id
where task.batchId = batch.id) as total,
(select sum(task.estimate ) from task where task.batchId = batch.id) as estimate
from batch
inner join task on task.batchId = batch.id
where batch_log.userId = @userId
Upvotes: 1