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

问题描述 投票:1回答:1

我正在使用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()返回销售编号的正确值,但是与这些销售编号相关联的销售人员的姓名不匹配。

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

mysql greatest-n-per-group mysql-5.7
1个回答
0
投票

您可以通过联合查询来满足此要求。下面的逻辑采用两个子查询的并集,这两个子查询分别查找每个学期/期间具有最低和最高薪水的记录。我们还包括一个称为position的计算列,该列跟踪每组中出现的两个记录之一是最小记录还是最大记录。对于给定学期/期间只有一个记录的情况,该记录将出现两次。

SELECT
    Nombre_Empleado,
    city,
    venta_por_empleado,
    orden_year,
    semestre,
    periodo
FROM
(
    SELECT
        t1.Nombre_Empleado,
        t1.city,
        t1.venta_por_empleado,
        t1.orden_year,
        t1.semestre,
        t1.periodo,
        1 AS position
    FROM tbl_ventas_ciudad_semestre t1
    INNER JOIN
    (
        SELECT
            semestre,
            city,
            MIN(venta_por_empleado) AS min_venta_por_empleado
        FROM tbl_ventas_ciudad_semestre
        GROUP BY
            semestre,
            city
    ) t2
        ON t2.semestre = t1.semestre AND
           t2.city = t1.city AND
           t2.min_venta_por_empleado = t1.venta_por_empleado

    UNION ALL

    SELECT
        t1.Nombre_Empleado,
        t1.city,
        t1.venta_por_empleado,
        t1.orden_year,
        t1.semestre,
        t1.periodo,
        2
    FROM tbl_ventas_ciudad_semestre t1
    INNER JOIN
    (
        SELECT
            semestre,
            city,
            MAX(venta_por_empleado) AS max_venta_por_empleado
        FROM tbl_ventas_ciudad_semestre
        GROUP BY
            semestre,
            city
    ) t2
        ON t2.semestre = t1.semestre AND
           t2.city = t1.city AND
           t2.max_venta_por_empleado = t1.venta_por_empleado
) t
ORDER BY
    semestre,
    periodo,
    position;
© www.soinside.com 2019 - 2024. All rights reserved.