我有一个看起来像这样的查询,它会产生一堆信息
mysql> SELECT impacted.incident_id AS id,
-> impacted.severity_id as sev,
-> ((UNIX_TIMESTAMP(incident.stable_time) - UNIX_TIMESTAMP(incident.start_time)) / 60) AS duration,
-> ops.department.name AS department,
-> ops.country.name AS country
-> FROM incident
-> LEFT JOIN incident_impactedDepartments AS impacted ON incident.incident_id = impacted.incident_id
-> LEFT JOIN incident_impacted ON incident.incident_id = incident_impacted.incident_id
-> LEFT JOIN ops.department ON impacted.dept_id = ops.department.dept_id
-> LEFT JOIN ops.country ON incident_impacted.entity_id = ops.country.country_id
-> WHERE incident.status in ('OPEN','STABLE','ALL CLEAR','POST MORTEM COMPLETE')
-> AND incident.incident_id in (35880,35992,33304);
+-------+------+-----------+-------------------+---------------+
| id | sev | duration | department | country |
+-------+------+-----------+-------------------+---------------+
| 33304 | 1 | 29.0000 | Marketing | NULL |
| 33304 | 1 | 29.0000 | Marketing | GLOBAL |
| 33304 | 1 | 29.0000 | Accounting | NULL |
| 33304 | 1 | 29.0000 | Accounting | GLOBAL |
| 35880 | 1 | 109.5833 | Marketing | Argentina |
| 35880 | 1 | 109.5833 | Marketing | Brazil |
| 35880 | 1 | 109.5833 | Marketing | Canada |
| 35880 | 1 | 109.5833 | Marketing | Chile |
| 35880 | 1 | 109.5833 | Marketing | Mexico |
| 35880 | 1 | 109.5833 | Marketing | Peru |
| 35880 | 1 | 109.5833 | Marketing | United States |
| 35880 | 1 | 109.5833 | Accounting | Argentina |
| 35880 | 1 | 109.5833 | Accounting | Brazil |
| 35880 | 1 | 109.5833 | Accounting | Canada |
| 35880 | 1 | 109.5833 | Accounting | Chile |
| 35880 | 1 | 109.5833 | Accounting | Mexico |
| 35880 | 1 | 109.5833 | Accounting | Peru |
| 35880 | 1 | 109.5833 | Accounting | United States |
| 35880 | 2 | 109.5833 | Finance | Argentina |
| 35880 | 2 | 109.5833 | Finance | Brazil |
| 35880 | 2 | 109.5833 | Finance | Canada |
| 35880 | 2 | 109.5833 | Finance | Chile |
| 35880 | 2 | 109.5833 | Finance | Mexico |
| 35880 | 2 | 109.5833 | Finance | Peru |
| 35880 | 2 | 109.5833 | Finance | United States |
| 35992 | 3 | 1295.2667 | Accounting | Italy |
+-------+------+-----------+-------------------+---------------+
您可以看到事件 35880 影响了 7 个国家的财务。我的目标是为每个事件收集以下信息。
+-------+------+----------+------------------------------+---------------------------------------------------------+
| id | sev | duration | department | country |
+-------+------+----------+------------------------------+---------------------------------------------------------+
| 35880 | 1 | 109.5833 | Marketing,Accounting,Finance | Argentina,Brazil,Canada,Chile,Mexico,Peru,United States |
+-------+------+----------+------------------------------+---------------------------------------------------------+
我可以通过这样的查询来做到这一点:
SELECT id, sev, duration, GROUP_CONCAT(DISTINCT department SEPARATOR ',') as department, GROUP_CONCAT(DISTINCT country SEPARATOR ',') as country
FROM
(SELECT impacted.incident_id AS id,
impacted.severity_id as sev,
((UNIX_TIMESTAMP(incident.stable_time) - UNIX_TIMESTAMP(incident.start_time)) / 60) AS duration,
ops.department.name AS department,
ops.country.name AS country
FROM incident
LEFT JOIN incident_impactedDepartments AS impacted ON incident.incident_id = impacted.incident_id
LEFT JOIN incident_impacted ON incident.incident_id = incident_impacted.incident_id
LEFT JOIN ops.department ON impacted.dept_id = ops.department.dept_id
LEFT JOIN ops.country ON incident_impacted.entity_id = ops.country.country_id
WHERE incident.status in ('OPEN','STABLE','ALL CLEAR','POST MORTEM COMPLETE')
AND incident.incident_id in (35880))
AS q1
但是被拉出的严重性是整个 id 35880 的严重性。看,35880 影响了不同级别的多个部门,我希望能够查看特定部门查询的严重性与主 ID 的严重性。但是,如果我在查询中指定部门 ID,就会破坏事情。
mysql> SELECT id, sev, duration, GROUP_CONCAT(DISTINCT department SEPARATOR ',') as department, GROUP_CONCAT(DISTINCT country SEPARATOR ',') as country
-> FROM
-> (SELECT impacted.incident_id AS id,
-> impacted.severity_id as sev,
-> ((UNIX_TIMESTAMP(incident.stable_time) - UNIX_TIMESTAMP(incident.start_time)) / 60) AS duration,
-> ops.department.name AS department,
-> ops.country.name AS country
-> FROM incident
-> LEFT JOIN incident_impactedDepartments AS impacted ON incident.incident_id = impacted.incident_id
-> LEFT JOIN incident_impacted ON incident.incident_id = incident_impacted.incident_id
-> LEFT JOIN ops.department ON impacted.dept_id = ops.department.dept_id
-> LEFT JOIN ops.country ON incident_impacted.entity_id = ops.country.country_id
-> WHERE incident.status in ('OPEN','STABLE','ALL CLEAR','POST MORTEM COMPLETE')
-> AND incident.incident_id in (35880)
-> AND impacted.dept_id = 1473)
-> AS q1;
+-------+------+----------+------------+---------------------------------------------------------+
| id | sev | duration | department | country |
+-------+------+----------+------------+---------------------------------------------------------+
| 35880 | 2 | 109.5833 | Finance | Argentina,Brazil,Canada,Chile,Mexico,Peru,United States |
+-------+------+----------+------------+---------------------------------------------------------+
我不再获得受影响部门的完整列表。
这需要两个单独的查询吗?