MySQL - 聚合来自两个表的信息

时间:2015-08-10 22:31:19

标签: mysql join

我努力将一张表中的摘要数字带入已经汇总数据的工作查询中。

我试图比较加班工作与加班时间的总分

超时引用的查询如下:

SELECT
    workorder_entry.workorder_id AS 'Work Order #',
    DATE_FORMAT((internalinvoice.invoice_date), '%b %e %Y') AS 'Invoice Date',
    SUM(ROUND((workorder_entry.adjusted_hours)*(workorder_entry.quantity),2)) AS 'Overtime Charged'
        FROM workorder_entry

        INNER JOIN service
        INNER JOIN servicecategory
        INNER JOIN internalinvoice  
        ON workorder_entry.service_id = service.ID
        AND service.servicecategory_id = servicecategory.ID
        AND internalinvoice.workorder_id = workorder_entry.workorder_id

        WHERE internalinvoice.invoice_date > CAST('2015-06-01 00:00:01' AS DATE)
        AND internalinvoice.invoice_date < CAST('2015-07-01 00:00:01' AS DATE)
        AND (workorder_entry.adjusted_hours)*(workorder_entry.quantity) >0
        AND servicecategory.ID <> 443

    GROUP BY internalinvoice.workorder_id
    ORDER BY internalinvoice.invoice_date ASC;

以上内容返回了在指定日期范围内开具发票的所有作业的列表,其中包含特定服务的加班时间摘要(即非443)。

输出如下:

Work Order #  | Invoice Date  |   Overtime Charged  
37755         | Jun 2 2015    |       10.16  
37754         | Jun 2 2015    |       18.00  
37753         | Jun 2 2015    |       25.00  

现在我想添加第4列,即上面工作单上所有加班时间的总和。这些列在jobcost_timesheet_entry表中,如下所示(由于多个人可能同时处理某个作业,因此重复日期):

workorder_id |Date Worked|   overtime  
37755        |May 15 2015|      9.50  
37755        |May 16 2015|      0.00  
37755        |May 17 2015|     12.25  
37755        |May 17 2015|      0.00  
37755        |May 17 2015|      1.75    
37755        |May 18 2015|      4.00
37754        |May 20 2015|      0.00
37753        |May 29 2015|     10.25
37753        |May 30 2015|      1.50

期望的输出:

Work Order #  | Invoice Date  | Overtime Charged  |Overtime Worked | Difference
37755         | Jun 2 2015    | 10.16             | 27.50          |     -17.34
37754         | Jun 2 2015    | 18.00             |  0.00          |      18.00
37753         | Jun 2 2015    | 25.00             | 11.75          |      14.25

我尝试了几种不同的东西,其中没有一种远离正确的信息。我怀疑答案可能是对第二组数据的子查询,但此时我有点迷失,而且这里的多次搜索并没有为这种情况提供良好的指导。

我非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

考虑将聚合组子查询添加为派生表(请参阅job_ot_total)。

SELECT 
    workorder_entry.workorder_id AS 'Work Order #', 
    DATE_FORMAT((internalinvoice.invoice_date), '%b %e %Y') AS 'Invoice Date'
    SUM(ROUND((workorder_entry.adjusted_hours)*(workorder_entry.quantity),2)) AS 'Overtime Charged',
    job_ot_total.SumOfOT AS 'Overtime Worked',
    (SUM(ROUND((workorder_entry.adjusted_hours)*(workorder_entry.quantity),2)) -
        job_ot_total.SumOfOT) AS 'Difference'
FROM workorder_entry
INNER JOIN service 
INNER JOIN servicecategory
INNER JOIN internalinvoice 
INNER JOIN (
        SELECT j.workerorder_id, Sum(j.overtime) AS SumOfOT
        FROM jobcost_timesheet_entry j
        GROUP BY j.workerorder_id
        ) AS job_ot_total
   ON workorder_entry.service_id = service.ID 
  AND service.servicecategory_id = servicecategory.ID 
  AND internalinvoice.workorder_id = workorder_entry.workorder_id  
  AND workorder_entry.workorder_id = job_ot_total.workerorder_id

WHERE internalinvoice.invoice_date > CAST('2015-06-01 00:00:01' AS DATE) 
  AND internalinvoice.invoice_date < CAST('2015-07-01 00:00:01' AS DATE) 
  AND (workorder_entry.adjusted_hours)*(workorder_entry.quantity) >0 
  AND servicecategory.ID <> 443

GROUP BY internalinvoice.workorder_id 
ORDER BY internalinvoice.invoice_date ASC;

或者,将job_ot_total保存为视图并将其合并到主查询中。有许多forum and blog discussions辩论更有效,可读和可维护:视图与派生表。

SELECT j.workerorder_id, Sum(j.overtime) AS SumOfOT
FROM jobcost_timesheet_entry j
GROUP BY j.workerorder_id