mysql - 在组上选择 MAX() 不会返回相应的同级列

标签 mysql greatest-n-per-group mysql-5.7

我正在使用 MySQL Tutorial's sample database .

我需要找到每个 学期 销售额最差(venta_por_empleado)和销售额最好(venta_por_empleado)的销售人员和 城市。使用temporary我获得以下结果集(tbl_ventas_ciudad_semestre,一个临时表):

| salesRepEmployeeNumber | Nombre_Empleado  | city          | venta_por_empleado | officeCode | orden_year | semestre | periodo |
|------------------------|------------------|---------------|--------------------|------------|------------|----------|---------|
| 1504                   | Barry Jones      | London        | 6719               | 7          | 2019       | 1        | 2019-1  |
| 1286                   | Foon Yue Tseng   | NYC           | 5016               | 3          | 2019       | 1        | 2019-1  |
| 1323                   | George Vanauf    | NYC           | 6372               | 3          | 2019       | 1        | 2019-1  |
| 1702                   | Martin Gerard    | Paris         | 4180               | 4          | 2019       | 1        | 2019-1  |
| 1401                   | Pamela Castillo  | Paris         | 8464               | 4          | 2019       | 1        | 2019-1  |
| 1370                   | Gerard Hernandez | Paris         | 12021              | 4          | 2019       | 1        | 2019-1  |
| 1166                   | Leslie Thompson  | San Francisco | 3587               | 1          | 2019       | 1        | 2019-1  |
| 1165                   | Leslie Jennings  | San Francisco | 11208              | 1          | 2019       | 1        | 2019-1  |
| 1611                   | Andy Fixter      | Sydney        | 5550               | 6          | 2019       | 1        | 2019-1  |
| 1621                   | Mami Nishi       | Tokyo         | 4923               | 5          | 2019       | 1        | 2019-1  |
| 1501                   | Larry Bott       | London        | 7776               | 7          | 2019       | 2        | 2019-2  |
| 1337                   | Loui Bondur      | Paris         | 6186               | 4          | 2019       | 2        | 2019-2  |
| 1188                   | Julie Firrelli   | Boston        | 4227               | 2          | 2020       | 1        | 2020-1  |
| 1612                   | Peter Marsh      | Sydney        | 6036               | 6          | 2020       | 1        | 2020-1  |
| 1216                   | Steve Patterson  | Boston        | 4876               | 2          | 2020       | 2        | 2020-2  |

要找到销售额最差的销售人员,我会将 MIN(venta_por_empleado) 应用到表中:

SELECT 
    Nombre_Empleado,
    city,
    MIN(venta_por_empleado) as venta_por_empleado,
    orden_year,
    semestre,
    periodo
FROM tbl_ventas_ciudad_semestre
GROUP BY
    city, periodo
ORDER BY
    periodo ASC, venta_por_empleado DESC;

结果:

| Nombre_Empleado | city          | venta_por_empleado | orden_year | semestre | periodo |
|-----------------|---------------|--------------------|------------|----------|---------|
| Barry Jones     | London        | 6719               | 2019       | 1        | 2019-1  |
| Foon Yue Tseng  | NYC           | 5016               | 2019       | 1        | 2019-1  |
| Martin Gerard   | Paris         | 4180               | 2019       | 1        | 2019-1  |
| Leslie Thompson | San Francisco | 3587               | 2019       | 1        | 2019-1  |
| Andy Fixter     | Sydney        | 5550               | 2019       | 1        | 2019-1  |
| Mami Nishi      | Tokyo         | 4923               | 2019       | 1        | 2019-1  |
| Larry Bott      | London        | 7776               | 2019       | 2        | 2019-2  |
| Loui Bondur     | Paris         | 6186               | 2019       | 2        | 2019-2  |
| Julie Firrelli  | Boston        | 4227               | 2020       | 1        | 2020-1  |
| Peter Marsh     | Sydney        | 6036               | 2020       | 1        | 2020-1  |
| Steve Patterson | Boston        | 4876               | 2020       | 2        | 2020-2  |

要找到销量最好的销售人员,我会将 MAX(venta_por_empleado) 应用到表中:

SELECT 
    Nombre_Empleado,
    city,
    MAX(venta_por_empleado) as venta_por_empleado,
    orden_year,
    semestre,
    periodo
FROM tbl_ventas_ciudad_semestre
GROUP BY
    city, periodo
ORDER BY
    periodo ASC, venta_por_empleado DESC;
| Nombre_Empleado | city          | venta_por_empleado | orden_year | semestre | periodo |
|-----------------|---------------|--------------------|------------|----------|---------|
| Barry Jones     | London        | 6719               | 2019       | 1        | 2019-1  |
| Foon Yue Tseng  | NYC           | 6372               | 2019       | 1        | 2019-1  |
| Martin Gerard   | Paris         | 12021              | 2019       | 1        | 2019-1  |
| Leslie Thompson | San Francisco | 11208              | 2019       | 1        | 2019-1  |
| Andy Fixter     | Sydney        | 5550               | 2019       | 1        | 2019-1  |
| Mami Nishi      | Tokyo         | 4923               | 2019       | 1        | 2019-1  |
| Larry Bott      | London        | 7776               | 2019       | 2        | 2019-2  |
| Loui Bondur     | Paris         | 6186               | 2019       | 2        | 2019-2  |
| Julie Firrelli  | Boston        | 4227               | 2020       | 1        | 2020-1  |
| Peter Marsh     | Sydney        | 6036               | 2020       | 1        | 2020-1  |
| Steve Patterson | Boston        | 4876               | 2020       | 2        | 2020-2  |

使用 MIN() 和 MAX() 它返回销售数字的正确值,但与这些销售数字关联的销售人员的姓名不匹配。

如何获得正确销售编号的正确名称?

最佳答案

这是一个可能既满足您的要求(或多或少)又可以工作的版本,因为 tbl_ventas_ciudad_semestre 是一个临时表。我们可以将您的表连接到一个子查询,该子查询可以找到每个学期和城市的最小和最大销售额:

SELECT
    t1.salesRepEmployeeNumber,
    t1.Nombre_Empleado,
    t1.city,
    t1.venta_por_empleado,
    t1.officeCode,
    t1.orden_year,
    t1.semestre,
    t1.periodo
FROM tbl_ventas_ciudad_semestre t1
INNER JOIN
(
    SELECT
        city,
        semestre,
        MIN(venta_por_empleado) AS min_venta_por_empleado,
        MAX(venta_por_empleado) AS max_venta_por_empleado
    FROM tbl_ventas_ciudad_semestre
    GROUP BY
        city,
        semestre
) t2
    ON t1.city = t2.city AND
       t1.semestre = t2.semestre AND
       t1.venta_por_empleado IN (t2.min_venta_por_empleado, t2.max_venta_por_empleado)
ORDER BY
    t1.semestre,
    t1.periodo,
    t1.venta_por_empleado;

关于mysql - 在组上选择 MAX() 不会返回相应的同级列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635968/

相关文章:

sql - T-SQL : How to select rows based on the max date?

mysql - SQL 选择 id=max(id) 的行

mysql - 获取特定标识符的最新行

mysql - 尽管 sql_mode 为空,存储过程仍引发 "incompatible with sql_mode=only_full_group_by"

mysql - 列出 MySQL 中所有具有不同 id 的重复名称

MySQL - 使用 SET 语句更新查询取决于前一个 SET 语句的结果

php - 当用户在 PayPal 结帐页面上关闭浏览器时执行操作

jdbc - Tomcat JDBC连接池,创建连接数大于maxActive

MySQL从跨行的任意长字符串数组中获取不同的值

php - 如何从数据库laravel中检索多个图像?