MySQL:根据两个不同的 ENUM 值从不同的行中选择最小值和最大值

MySQL: Selecting min and max values from different rows depending two different ENUM values

好吧,这个标题可能令人费解,所以让我尝试通过显示一些 table 和查询来解释我的问题。我有一个 table '_commision',其中包含以下行:

+-------+-------------+------+----------------+----------------+-----------+
| id    | relation_id | type | min_commission | max_commission | percental |
+-------+-------------+------+----------------+----------------+-----------+
| 22892 |        3427 | SALE |           1.40 |           1.80 | yes       |
| 22891 |        3427 | SALE |          30.00 |          60.00 | no        |
| 21075 |        6365 | LEAD |          30.00 |           NULL | no        |
| 19638 |        4436 | SALE |           1.10 |           NULL | yes       |
| 19637 |        4436 | LEAD |          30.00 |           NULL | no        |
+-------+-------------+------+----------------+----------------+-----------+
5 rows in set (0.00 sec)

我需要获得绝对 min_commission 值和绝对 max_commission 值。棘手的部分是百分比列,稍后您将看到。我的第一个想法是这样的:

SELECT rc.type, rc.currency_id, rc.percental, 
    MIN( rc.min_commission ) AS min_commission, 
    IF( MAX(GREATEST( rc.min_commission, rc.max_commission )) > MIN( rc.min_commission ) , MAX(GREATEST( rc.min_commission, rc.max_commission )) , 0.00 ) AS max_commission
FROM _commission rc
LEFT JOIN ...
GROUP BY rc.type;

此查询产生以下行:

+------+-------------+-----------+----------------+----------------+
| type | currency_id | percental | min_commission | max_commission |
+------+-------------+-----------+----------------+----------------+
| LEAD |           1 | no        |          30.00 |           0.00 |
| SALE |           1 | no        |           1.10 |          60.00 |
+------+-------------+-----------+----------------+----------------+
2 rows in set (0.00 sec)

但是,我需要得到一个考虑列 'percental' 的结果,因为如果类型是 'SALE',佣金也可以是 1.4% 和 30 美元(例如百分比或固定).如您所见,我需要如下结果,但无法获得 suitable 查询。结果应该是这样的:

+------+-------------+-----------+----------------+----------------+
| type | currency_id | percental | min_commission | max_commission |
+------+-------------+-----------+----------------+----------------+
| LEAD |           1 | no        |          30.00 |           0.00 |
| SALE |           1 | yes       |           1.10 |           1.80 |
| SALE |           1 | no        |          30.00 |          60.00 |
+------+-------------+-----------+----------------+----------------+

有什么想法吗?

试试这个,

    SELECT 
            rc.type, rc.currency_id, rc.percental, 
            MIN( rc.min_commission ) AS min_commission, 
            IF( MAX(GREATEST( rc.min_commission, rc.max_commission )) 
            > 
            MIN( rc.min_commission ) , MAX(GREATEST( rc.min_commission, rc.max_commission )) , 0.00 ) AS max_commission
    FROM 
            _commission rc
    LEFT JOIN ...
    GROUP BY 
            rc.type, rc.percental;