您好我正在寻找一种解决方案,让我无法理解如何在查询中获得整列总数。
此查询获取工程师姓名和他们拥有的超出SLA的工作数,即工作应该已完成的数据已经过去,工作仍有待完成。
SELECT Engineer,Job_Status,COUNT(*) as 'Out Of SLA'
FROM import
WHERE (Job_Status = 'P' or Job_Status='P2' or Job_Status='P8')
and (isnull(Job_Completed_Date)
or Job_Completed_Date='0000-00-00')
and (Job_SLA_Due_Date < CURDATE()
)
GROUP BY import.Engineer,Job_Status
上面的代码从导入表中产生以下结果。
+----------------+------------+------------+
| Engineer | Job_Status | Out of SLA |
+----------------+------------+------------+
| Andy Beeres | P | 15 |
| Andy Broad | P | 4 |
| Darren Goodwin | P | 6 |
+----------------+------------+------------+
我希望能够显示Out of SLA列的总个数以及表数据的其余部分,如果有意义的话,如下表所示。
| Engineer | Job_Status | Out of SLA |
|------------- |------------ |------------ |
| Andy Beeres | P | 14 |
| | P2 | 3 |
| | P8 | 1 |
| Total | | 18 |
| Andy Broad | P | 12 |
| | P2 | 2 |
| Total | | 14 |
| Grand Total | | 32 |
此致
艾伦
答案 0 :(得分:2)
将with rollup
与group by一起使用以获取total_sla
根据MySql Docs:
GROUP BY子句允许WITH ROLLUP修饰符,该修饰符使得摘要输出包含表示更高级别(即超级聚合)摘要操作的额外行。因此,ROLLUP使您能够通过单个查询回答多个分析级别的问题。
SELECT Engineer,Job_Status,COUNT(*) as 'Out Of SLA'
FROM import
WHERE (Job_Status = 'P' or Job_Status='P2' or Job_Status='P8')
and (isnull(Job_Completed_Date)
or Job_Completed_Date='0000-00-00')
and (Job_SLA_Due_Date < CURDATE()
)
GROUP BY import.Engineer,Job_Status WITH ROLLUP
答案 1 :(得分:-1)
一种选择是使用查找SLA总数的子查询:
SELECT Engineer,
Job_Status,
COUNT(*) AS `Out Of SLA`,
(SELECT COUNT(*) FROM import) AS total_sla
FROM import
WHERE (Job_Status = 'P' OR Job_Status='P2' OR Job_Status='P8') AND
(ISNULL(Job_Completed_Date) OR Job_Completed_Date = '0000-00-00') AND
Job_SLA_Due_Date < CURDATE()
GROUP BY Engineer,
Job_Status