在同一个表上加入2个查询,并将列添加到结果

时间:2017-08-09 14:45:17

标签: mysql join

我是MYSQL的新手,需要帮助才能完成查询。

我有两个单独的查询,我需要将其放入一个我可以在PHP中使用的查询中。

我使用以下SQL来获取名为' import'的表中的记录数。 Job_Completed_Date小于或等于Job_SLA_Due_Date。

我的where子句检查' import'中过去6个月内完成的工作。表。

SELECT Year(job_completed_date), 
       Month(job_completed_date), 
       Count(*) AS Completed_On_Schedule 
FROM   import 
WHERE  job_completed_date >= Last_day(Now()) + interval 1 day - interval 6 month 
       AND job_completed_date <= job_sla_due_date 
       AND ( job_status = 'C' 
              OR job_status = 'C2' 
              OR job_status = 'C8' ) 
GROUP  BY Year(job_completed_date), 
          Month(job_completed_date) 
ORDER  BY job_completed_date ASC 

结果输出正常并产生以下内容....

Year    Month  Completed_On_Schedule 
2017    3      1155 
2017    4      838 
2017    5      881 
2017    6      1095 
2017    7      1073 
2017    8      295 

我的第二个查询类似于使用相同的表,但我正在寻找过去6个月内已完成的作业总数,但我并不担心Job_SLA_Due_Date。我正在使用的SQL是.......

SELECT Year(job_completed_date), 
       Month(job_completed_date), 
       Count(*) AS Total_Completed 
FROM   import 
WHERE  job_completed_date >= Last_day(Now()) + interval 1 day - interval 6 month 
       AND ( job_status = 'C' 
              OR job_status = 'C2' 
              OR job_status = 'C8' ) 
GROUP  BY Year(job_completed_date), 
          Month(job_completed_date) 
ORDER  BY job_completed_date ASC 

结果输出符合预期并产生以下结果.....

    Year  Month  Total_Completed 
    2017  3      1441 
    2017  4      1101 
    2017  5      1144 
    2017  6      1334 
    2017  7      1211 
    2017  8      311 

我现在需要的是输出......

    Year   Month Completed_On_Schedule Total_Completed %_On_Schedule 
    2017    3    1155                  1441            80.1 
    2017    4    838                   1101            76.1 
    2017    5    881                   1144            77 
    2017    6    1095                  1334            82 
    2017    7    1073                  1211            88.6 
    2017    8    295                   311             94.8 

因此,简而言之,我需要将我的2个查询连接在一起并添加另一个名为%_Completed_On_Schedule的列,该列是按计划完成的作业的百分比,即Completed_On_Schedule列与Total_Completed列的百分比。

任何帮助都将非常感激。

问候

艾伦

3 个答案:

答案 0 :(得分:1)

只需在COUNT上使用条件SELECT即可。

使用IN代替多个OR

的附加提示
SELECT Year(job_completed_date), 
       Month(job_completed_date), 
       COUNT(CASE WHEN job_completed_date <= job_sla_due_date 
                  THEN 1 END) as Completed_On_Schedule,
       COUNT(*) AS Total_Completed,
       COUNT(CASE WHEN job_completed_date <= job_sla_due_date 
                  THEN 1 END) * 100.0 / 
       COUNT(*) AS Total_Completed as `%_On_Schedule`

FROM   import 
WHERE  job_completed_date >= Last_day(Now()) + interval 1 day - interval 6 month 
       AND job_status IN ('C', 'C2', 'C8') 
GROUP  BY Year(job_completed_date), 
          Month(job_completed_date) 
ORDER  BY job_completed_date ASC 

为了回答如何将两个查询连接在一起这里是一个例子(即使在这种情况下你不需要它):

SELECT Q1.*, Q2.*
FROM ( <your first query> ) as Q1
JOIN ( <your second query> ) as Q2
  ON (Q1.year, Q1.month) = (Q2.year, Q2.month)

答案 1 :(得分:0)

我在您的查询中添加了一个包含total的列和一个CASE clausule。此外,我已经删除了SLA的AND,因为我们在CASE _

中有它
SELECT Year(job_completed_date), 
       Month(job_completed_date), 
       SUM(CASE 
               WHEN job_completed_date <= job_sla_due_date THEN 1 ELSE 0
           END) AS Completed_On_Schedule,
       Count(*) AS Total_Completed
FROM   import 
WHERE  job_completed_date >= Last_day(Now()) + interval 1 day - interval 6 month  
       AND ( job_status = 'C' 
              OR job_status = 'C2' 
              OR job_status = 'C8' ) 
GROUP  BY Year(job_completed_date), 
          Month(job_completed_date) 
ORDER  BY job_completed_date ASC 

答案 2 :(得分:0)

为简单起见,我将您的查询称为QUERY_ONEQUERY_TWO,您当然需要将这些引用替换为实际查询。

SELECT a.Year, a.Month, a.Completed_On_Schedule, b.Total_Completed, 
((a.Completed_On_Schedule / b.Total_Completed) * 100) AS `%_On_Schedule` 
FROM (QUERY_ONE) AS a JOIN (QUERY_TWO) AS b ON a.Year = b.Year AND a.Month = b.Month;

此查询将在“年”和“月”上匹配的行上连接两个表,然后从每个表中提取所需的值并创建计划百分比列。你需要对你的%_On_Schedule名称应用滴答,因为%是MySQL中的一个关键术语,我建议不要因为这个原因使用这个符号,但它确实不是什么大问题。