对单个派生表进行多个查询?
我有一个如下所示的查询,它会生成大量信息
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 |
+-------+------+----------+------------+---------------------------------------------------------+
我不再获得受影响部门的完整列表。
这需要两个单独的查询吗?
I have a query that looks like this and it yields a bunch of information
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 |
+-------+------+-----------+-------------------+---------------+
You can see that incident 35880 impacts 7 countries for Finance. My goal is to gather the following info for each incident.
+-------+------+----------+------------------------------+---------------------------------------------------------+
| id | sev | duration | department | country |
+-------+------+----------+------------------------------+---------------------------------------------------------+
| 35880 | 1 | 109.5833 | Marketing,Accounting,Finance | Argentina,Brazil,Canada,Chile,Mexico,Peru,United States |
+-------+------+----------+------------------------------+---------------------------------------------------------+
And I can do so with a query like this:
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
But the severity being pulled is the severity for id 35880 as a whole. See, 35880 impacted multiple departments on a different level and I want to be able to see the severity for the specific department querying versus the master id's severity. However, if I specify the department ID in the query, that breaks things.
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 |
+-------+------+----------+------------+---------------------------------------------------------+
I no longer get the complete list of impacted departments.
Will this require two separate queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我完全理解您的查询,但我希望以下任一查询可能有所帮助:
查询 1:
查询 2:
我希望上述查询之一应给出您期望的结果。
I'm not sure I fully understand your query, but I hope either of the below might help:
Query 1:
Query 2:
I hope that one of the above queries should give results as expected by you.