简体   繁体   English

SQL关系

[英]SQL relationships

I have four tables: 我有四个表:

  1. task - have a batch_id and estimates of how long a task would take to complete 任务-具有batch_id并估算任务完成所需的时间
  2. batches - groups of tasks 批处理-任务组
  3. batch_log - entries showing the time worked for each task with a userid for who worked it. batch_log-显示每个任务的工作时间的条目,并带有一个用户ID(工作人员)。
  4. operation - type of machine the batch is run on, paint, silkscreen, etc. 操作-批次运行的机器类型,油漆,丝网印刷等。

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? 如何使每个用户的每个批次工作,这些批次ID的每个批次日志的总消耗量以及该批次的每个任务ID的总估算值?

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. 这样做的理由是,可以对用户的生产率进行评分,并在excel中使用这些查询。 I think I may have to go with two queries: 我想我可能必须要考虑两个问题:

  1. the batch log 批处理日志
  2. a query to get the total estimated for each batch 查询以获取每个批次的估计总数

select distinct bl.userid , t.batchid from batch_log bl inner join task t on t.taskid = bl.taskid ; 从t.taskid = bl.taskid上的batch_log bl内部联接任务t中选择不同的bl.userid和t.batchid;

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; 从批处理b中选择sum(bl.time_spent),b.batchid在b.batchid = t.batchid内联接任务t上在bl.taskid = t.taskid上进行内部联接batch_log bl;

select sum(t.estimate) , b.batchid from batch b left join task t on b.batchid = t.batchid ; 从b.batchid = t.batchid的批处理b左连接任务t中选择sum(t.estimate),b.batchid;

Why is it called batch_log but it is about taskes and time spent ? 为什么将其称为batch_log,但它涉及任务和所花费的时间?

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

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. 没有任何表结构可以说很难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM