SQL关系

时间:2011-04-19 15:55:52

标签: sql relationships

我有四张桌子:

  1. 任务 - 拥有batch_id并估算任务完成所需的时间
  2. 批次 - 任务组
  3. batch_log - 显示每个任务的工作时间的条目,其中包含使用用户ID的人员。
  4. 操作 - 批次运行的机器类型,油漆,丝网印刷等
  5. 我如何为每个用户分别处理每个批处理,从这些批处理ID的每个批处理日志中经过的总数以及该批处理的每个任务ID的总估计值?

    编辑:

    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
    

    我在想:

    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
    

    这样做的原因是可以向用户评估他们的工作效率,并在excel中使用这些查询。我想我可能不得不提出两个问题:

    1. 批日志
    2. 获取每批次估算总额的查询

3 个答案:

答案 0 :(得分:1)

从t_taskid = bl.taskid上的batch_log bl内部联接任务t中选择明显的bl.userid,t.batchid;

选择sum(bl.time_spent),b.batchid来自批次b左连接任务t on b.batchid = t.batchid inner join batch_log bl on bl.taskid = t.taskid;

选择sum(t.estimate),b。batchid来自批次b左连接任务t on b.batchid = t.batchid;

为什么它被称为batch_log,但它是关于任务和花费的时间?

答案 1 :(得分:1)

我不确定你们桌子的结构,但是这样的事情应该有效:

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

答案 2 :(得分:1)

类似的东西:

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

很难说没有任何类型的表结构。