将组中的最大值附加到结果的每一行?
Attach max from group to every row of results ?
我有类似下面的数据库结构:
应用服务 table :
+-----+--------------+----------+---------------+
| id | name | status | application_id
| 1 | Service 1 | 1 | 24
| 2 | Service 2 | 2 | 24
| 3 | Service 3 | 3 | 25
+-----+--------------+----------+----------------
还有其他 table 具有状态定义:
客户状态
+------------+--------------+----------+-----------+
| status_id | name | level | is_closed
| 1 | Status 1 | 2 | 1
| 2 | Status 2 | 1 | 0
| 3 | Status 3 | 3 | 1
+------------+--------------+----------+----------
ApplicationServices 的每一行的状态计算为按 application_id 分组的记录中的最大级别状态。
因此,要从 ApplicationServices 中获取所有具有状态的记录,结果将如下所示:
+-----+--------------+----------+----------------+-------------+-----------
| id | name | status | application_id | status_name | is_closed
| 1 | Service 1 | 1 | 24 | Status 1 | 1
| 2 | Service 2 | 2 | 24 | Status 1 | 1
| 3 | Service 3 | 3 | 25 | Status 3 | 1
+-----+--------------+----------+----------------+-------------+-----------
是否有有效的方法将按 application_id 分组的 max(level) 结果附加到结果集的每一行?
试试这个:
SELECT A.id, A.name, A.status, A.application_id, CS.name AS status_name, CS.is_closed
FROM ApplicationService A
INNER JOIN (SELECT AA.application_id, MAX(CS.level) AS maxLevel
FROM ApplicationService AA
INNER JOIN CustomerStatus CS ON AA.status = CS.status_id
GROUP BY AA.application_id
) AS AA ON A.application_id = AA.application_id
INNER JOIN CustomerStatus CS ON AA.maxLevel = CS.level;
您可能必须执行子查询。子查询只是将两个表连接在一起,grouping/collapsing by application_id
然后获取最大的 is_closed
值:
SELECT
t1.status AS `status`,
t1.application_id AS `app_id`,
MAX(t2.is_closed) AS `max_is_closed`
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
GROUP BY t1.application_id
当您合并子查询时,最大 is_closed
值应该可以通过 max_is_closed
别名访问:
SELECT
t1.id AS id,
t1.name AS name,
t1.status AS `status`,
t1.application_id AS application_id,
t2.name AS status_name,
t3.max_is_closed
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
LEFT JOIN
(SELECT
t1.status AS `status`,
t1.application_id AS `app_id`,
MAX(t2.is_closed) AS `max_is_closed`
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
GROUP BY t1.application_id) AS t3 ON
t1.application_id = t3.app_id
查询的输出:
我有类似下面的数据库结构: 应用服务 table :
+-----+--------------+----------+---------------+
| id | name | status | application_id
| 1 | Service 1 | 1 | 24
| 2 | Service 2 | 2 | 24
| 3 | Service 3 | 3 | 25
+-----+--------------+----------+----------------
还有其他 table 具有状态定义: 客户状态
+------------+--------------+----------+-----------+
| status_id | name | level | is_closed
| 1 | Status 1 | 2 | 1
| 2 | Status 2 | 1 | 0
| 3 | Status 3 | 3 | 1
+------------+--------------+----------+----------
ApplicationServices 的每一行的状态计算为按 application_id 分组的记录中的最大级别状态。
因此,要从 ApplicationServices 中获取所有具有状态的记录,结果将如下所示:
+-----+--------------+----------+----------------+-------------+-----------
| id | name | status | application_id | status_name | is_closed
| 1 | Service 1 | 1 | 24 | Status 1 | 1
| 2 | Service 2 | 2 | 24 | Status 1 | 1
| 3 | Service 3 | 3 | 25 | Status 3 | 1
+-----+--------------+----------+----------------+-------------+-----------
是否有有效的方法将按 application_id 分组的 max(level) 结果附加到结果集的每一行?
试试这个:
SELECT A.id, A.name, A.status, A.application_id, CS.name AS status_name, CS.is_closed
FROM ApplicationService A
INNER JOIN (SELECT AA.application_id, MAX(CS.level) AS maxLevel
FROM ApplicationService AA
INNER JOIN CustomerStatus CS ON AA.status = CS.status_id
GROUP BY AA.application_id
) AS AA ON A.application_id = AA.application_id
INNER JOIN CustomerStatus CS ON AA.maxLevel = CS.level;
您可能必须执行子查询。子查询只是将两个表连接在一起,grouping/collapsing by application_id
然后获取最大的 is_closed
值:
SELECT
t1.status AS `status`,
t1.application_id AS `app_id`,
MAX(t2.is_closed) AS `max_is_closed`
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
GROUP BY t1.application_id
当您合并子查询时,最大 is_closed
值应该可以通过 max_is_closed
别名访问:
SELECT
t1.id AS id,
t1.name AS name,
t1.status AS `status`,
t1.application_id AS application_id,
t2.name AS status_name,
t3.max_is_closed
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
LEFT JOIN
(SELECT
t1.status AS `status`,
t1.application_id AS `app_id`,
MAX(t2.is_closed) AS `max_is_closed`
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
GROUP BY t1.application_id) AS t3 ON
t1.application_id = t3.app_id
查询的输出: