对单个派生表进行多个查询?

发布于 2024-12-13 19:20:39 字数 6117 浏览 0 评论 0原文

我有一个如下所示的查询,它会生成大量信息

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-12-20 19:20:39

我不确定我完全理解您的查询,但我希望以下任一查询可能有所帮助:

查询 1:

SELECT id, sev, sev1, 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, impacted1.severity_id as sev1, ((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_impactedDepartments AS impacted1 ON incident.incident_id = impacted1.incident_id AND impacted1.dept_id = 1473
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;

查询 2:

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
GROUP BY department;

我希望上述查询之一应给出您期望的结果。

I'm not sure I fully understand your query, but I hope either of the below might help:

Query 1:

SELECT id, sev, sev1, 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, impacted1.severity_id as sev1, ((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_impactedDepartments AS impacted1 ON incident.incident_id = impacted1.incident_id AND impacted1.dept_id = 1473
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;

Query 2:

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
GROUP BY department;

I hope that one of the above queries should give results as expected by you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文