在组上选择 MAX() 不会 return 相应的同级列

Selecting MAX() on a group doesn't return the corresponding sibling columns

我正在使用 MySQL Tutorial's sample database

我需要为每个 semestrecity 找到销售额最差(最低 venta_por_empleado)和销售额最好(最高 venta_por_empleado)的销售人员。使用 temporary tables 我获得以下结果集(tbl_ventas_ciudad_semestre,临时 table):

| 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) 应用到 table:

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) 应用到 table:

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() returns 销售数字的正确值,但与这些销售数字相关联的销售人员的姓名不匹配。

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

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

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;

考虑到每个 table 的约束都是临时的,我需要创建一个新的临时 table 来按照 Tim 的建议从 tbl_ventas_ciudad_semestre 复制内容。

DROP TEMPORARY TABLE IF EXISTS tbl_ventas_ciudad_semestre_2;
CREATE TEMPORARY TABLE tbl_ventas_ciudad_semestre_2
    SELECT * FROM tbl_ventas_ciudad_semestre;

然后可以使用子查询

获得第一个 table 的正确 MIN 和 MAX
/* MIN or worst sales*/
    SELECT * FROM tbl_ventas_ciudad_semestre a WHERE
    a.venta_por_empleado IN 
    (
    SELECT MIN(b.venta_por_empleado) FROM tbl_ventas_ciudad_semestre_2 b
    GROUP BY periodo, city
    );
/* MAX or best sales*/
    SELECT * FROM tbl_ventas_ciudad_semestre a WHERE
    a.venta_por_empleado IN 
    (
    SELECT MAX(b.venta_por_empleado) FROM tbl_ventas_ciudad_semestre_2 b
    GROUP BY periodo, city
    )