嗯,这个标题可能很令人困惑,所以让我试着通过展示一些表格和查询来解释我的问题。我有一个'_commision'表,其中包含以下行:
INSERT INTO table_name (date_column) VALUE ('YYYY-MM-DD');
我需要获取绝对min_commission值和绝对max_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)
此查询会产生以下行:
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;
但是,我需要得到一个结果,考虑列'百分比',因为如果类型为'SALE',佣金可以是1.4%和30美元(例如百分比或修复)。正如您所看到的,我需要一个像下面这样的结果,但我无法得到合适的查询。这就是结果的样子:
+------+-------------+-----------+----------------+----------------+
| 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)
有什么想法吗?
答案 0 :(得分:1)
试试这个,
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;