我想根据日期计算product_mrp
的最大差值。为此,我试图按日期分组,但此后无法获得。
输入:
+-------------+--------------------+
| product_mrp | order_date |
+-------------+--------------------+
| 142 | 01-12-2019 |
| 20 | 01-12-2019 |
| 20 | 01-12-2019 |
| 120 | 01-12-2019 |
| 30 | 03-12-2019 |
| 20 | 03-12-2019 |
| 45 | 03-12-2019 |
| 215 | 03-12-2019 |
| 15 | 03-12-2019 |
| 25 | 07-12-2019 |
| 5 | 07-12-2019 |
+-------------+--------------------+
期望的输出:
+-------------+--------------------+
| product_mrp | order_date |
+-------------+--------------------+
| 122 | 01-12-2019 |
| 200 | 03-12-2019 |
| 20 | 07-12-2019 |
+-------------+--------------------+
order_date
列,并计算product_mrp
之间的差。然后将其分组并获得最大值。SELECT MAX(t1.product_mrp - t2.product_mrp) AS product_mrp, t1.order_date
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.order_date = t2.order_date AND t1.product_mrp >= t2.product_mrp
GROUP BY t1.order_date