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

时间:2019-10-31 01:58:10

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

我正在使用MySQL Tutorial's sample database

我需要找到每个venta_por_empleadoventa_por_empleado的销售额最低(semestre最低)和销售额最高(city最高)的销售人员。使用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()返回正确的销售编号值,但是与这些销售编号相关联的销售人员的姓名不匹配。

如何获取正确的销售号码的正确名称?

2 个答案:

答案 0 :(得分:0)

这是一个既可以满足您的要求(或多或少),又可以将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;

答案 1 :(得分:0)

鉴于每个表都是临时表的约束,我需要创建一个新的临时表以按照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;

然后可以使用子查询获取第一个表的正确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
    )