【问题标题】:SQL relationshipsSQL 关系
【发布时间】:2011-08-08 19:37:31
【问题描述】:

我有四张桌子:

  1. task - 有一个 batch_id 和估计完成任务需要多长时间
  2. 批次 - 任务组
  3. batch_log - 条目显示每个任务的工作时间,并带有工作人员的用户 ID。
  4. 操作 - 批次运行的机器类型、油漆、丝网印刷等。

我如何让每个用户的每个批次工作、每个批次日志中这些批次 ID 的总运行时间以及该批次的每个任务 ID 的总估计值?

编辑:

表格:

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. 用于获取每批次的估计总数的查询

【问题讨论】:

    标签: sql relationships


    【解决方案1】:

    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 ;

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

    【讨论】:

    • 因为单独的 time_elapsed 不计入任务,而是计入这些任务所在的整个批次,但每个任务应该持续多长时间的估计值是在单独的任务上。
    【解决方案2】:

    类似:

    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
    

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

    【讨论】:

      【解决方案3】:

      我不确定你的表格的结构,但是这样的东西应该可以工作:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多