【问题标题】:Attach max from group to every row of results ?将组中的最大值附加到每一行结果?
【发布时间】:2015-12-18 10:41:05
【问题描述】:

我有类似如下的数据库结构: 应用服务表:

+-----+--------------+----------+---------------+
|  id | name         | status   | application_id
|  1  | Service 1    |  1       |     24
|  2  | Service 2    |  2       |     24
|  3  | Service 3    |  3       |     25
+-----+--------------+----------+----------------

还有其他带有状态定义的表: 客户状态

+------------+--------------+----------+-----------+
|  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
+-----+--------------+----------+----------------+-------------+-----------

有没有一种有效的方法可以将 max(level) 按 application_id 分组的结果附加到结果集的每一行?

【问题讨论】:

    标签: mysql sql select group-by greatest-n-per-group


    【解决方案1】:

    试试这个:

    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;
    

    【讨论】:

    • 这正是我所需要的,实际上几乎是我以前的两倍。谢谢!
    【解决方案2】:

    See working SQLfiddle here.

    您可能必须执行子查询。子查询只需将两个表连接在一起,按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
    

    查询的输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 2023-03-31
      • 2018-08-05
      • 1970-01-01
      • 2019-03-22
      • 2015-01-23
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多