按日期计算列值的范围

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

enter image description here

我想根据日期计算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 |
+-------------+--------------------+
python pandas dataframe group-by date-difference
1个回答
0
投票
您需要将表与其自身连接到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

© www.soinside.com 2019 - 2024. All rights reserved.