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

标签 mysql enums dependencies max min

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

+-------+-------------+------+----------------+----------------+-----------+
| 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)

但是,我需要得到一个考虑“百分比”列的结果,因为如果类型为“销售”,则佣金也可以是 1.4% 和 30 美元(百分比或固定)。正如您所看到的,我需要如下结果,但我无法获得合适的查询。结果应该是这样的:

+------+-------------+-----------+----------------+----------------+
| 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;

关于MySQL:根据两个不同的ENUM值从不同的行中选择最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37183161/

相关文章:

php - Highchart JSON饼图显示 Slice undefined

swift - 快速将枚举转换为 NSNumber

c# - 将枚举序列化为字符串时,是否可以添加新的枚举成员而不破坏与 Newtonsoft json 的契约(Contract)?

c# - 什么时候需要依赖 dll?

MySQL View 创建

mysql - 如何在 MySQL 的列中查找重复次数最多的单词?

scala - 为什么 scalac 需要类路径的传递依赖

android - 如何使用 Gradle 版本目录知道是否存在依赖项更新

mysql - 为什么会出现这种情况?

java - Android 为什么所有的布局都是用字符串而不是枚举来完成