将两行合并到新列mysql中

时间:2017-03-15 03:43:26

标签: mysql sql

我正在运行一个提供结果集的SQL查询,如下所示。我正在合并来自jobentry(适用于正常工作时间的员工)和overtimejobentry(适用于加班工作的员工)的结果。

我想要的是组合具有相同jobNumberwbsCode的行,以便正常小时和加班时间出现在每个作业和代码的同一行中。

有没有办法可以将两行合并为一个并为其他数据创建一个新列?

SQL查询:

SELECT 
    fk_jobNumber AS jobNumber,
    wbsCode,
    SUM(tue + wed + thu + fri + mon) AS totalHours
FROM
    jobentry
        INNER JOIN
    task ON task.taskID = jobentry.fk_taskID
        INNER JOIN
    wbscodeitem ON wbscodeitem.wbsCodeItemID = task.fk_wbsCodeItemID
        INNER JOIN
    job ON job.jobNumber = wbscodeitem.fk_jobNumber
GROUP BY fk_jobNumber , wbsCode 

UNION

/*Same SQL statement as above except it's for table overtimejobentry instead of jobentry*/
SELECT 
    fk_jobNumber AS jobNumber,
    wbsCode,
    SUM(tue + wed + thu + fri + sat + sun + mon) AS totalHours
FROM
    overtimejobentry
        INNER JOIN
    task ON task.taskID = overtimejobentry.fk_taskID
        INNER JOIN
    wbscodeitem ON wbscodeitem.wbsCodeItemID = task.fk_wbsCodeItemID
        INNER JOIN
    job ON job.jobNumber = wbscodeitem.fk_jobNumber
GROUP BY fk_jobNumber , wbsCode

结果:

jobNumber   wbsCode   totalHours
--------------------------------
10             88            25   (from jobentry table)
10            552            70   (from jobentry table)
25             17           150   (from jobentry table)
25            374            38   (from jobentry table)
10             88            63   (from overtimejobentry table)
25            374            12   (from overtimejobentry table)

这就是我要做的事情:

jobNumber   wbsCode   totalHours   totalOvertimeHours
----------------------------------------------------- 
10             88            25                    63
10            552            70                     0
25             17           150                     0
25            374            38                    12

1 个答案:

答案 0 :(得分:1)

您可以使用数据透视表将两种类型的小时数分别用于每个作业编号和WBS代码的单个记录。在下面的查询中,我添加了一个名为type的计算列,它可以跟踪正常时间或加班时间的来源。

SELECT t.jobNumber,
       t.wbsCode,
       MAX(CASE WHEN t.type = 'normal'   THEN totalHours END) AS totalHours,
       MAX(CASE WHEN t.type = 'overtime' THEN totalHours END) AS totalOvertimeHours
FROM
(
    SELECT fk_jobNumber AS jobNumber,
           wbsCode,
           SUM(tue + wed + thu + fri + mon) AS totalHours,
           'normal' AS type      -- remember which records come from normal hours
    FROM jobentry
    INNER JOIN task
        ON task.taskID = jobentry.fk_taskID
    INNER JOIN wbscodeitem
        ON wbscodeitem.wbsCodeItemID = task.fk_wbsCodeItemID
    INNER JOIN job
        ON job.jobNumber = wbscodeitem.fk_jobNumber
    GROUP BY fk_jobNumber, wbsCode 

    UNION        -- note: you might want to use UNION ALL here

    SELECT fk_jobNumber AS jobNumber,
           wbsCode,
           SUM(tue + wed + thu + fri + sat + sun + mon) AS totalHours,
           'overtime' AS type    -- remember which records come from overtime hours
    FROM overtimejobentry
    INNER JOIN task
        ON task.taskID = overtimejobentry.fk_taskID
    INNER JOIN wbscodeitem
        ON wbscodeitem.wbsCodeItemID = task.fk_wbsCodeItemID
    INNER JOIN job
        ON job.jobNumber = wbscodeitem.fk_jobNumber
    GROUP BY fk_jobNumber, wbsCode
) t
GROUP BY t.jobNumber,
         t.wbsCode

请注意,如上所述,使用子查询进行换行的可能替代方法是将两个子查询连接在一起。这里的问题是给定的作业号和WBS代码组合可能出现在一个中,而不是另一个。在这种情况下,我们可能不得不求助于完全外连接,MySQL没有内置支持。上面给出的答案很可能更容易阅读和维护。