【发布时间】:2021-06-01 23:20:56
【问题描述】:
我需要根据“更新”列中的值计算赔率之间的差异,此时我将更新值为最小值的赔率减去更新值为最大值的赔率。它工作得很好,但我刚刚意识到在某些列中有时恰好为 0,我想知道是否可以根据更新的列选择最小值,并且只选择高于 0 的值。
桌子是这样的
| fixture_id | H_odds | D_odds | A_odds | ev_tstamp | updated |
|---|---|---|---|---|---|
| 120000 | 1.40 | 1.50 | 1.30 | 132000 | 12 |
| 120000 | 1.10 | 1.10 | 1.10 | 132000 | 11 |
| 120000 | 1.20 | 0 | 1.60 | 132000 | 10 |
这就是我想要的回报
| fixture_id | H_odds | D_odds | A_odds | ev_tstamp | updated | dif_h | dif_d | dif_a |
|---|---|---|---|---|---|---|---|---|
| 120000 | 1.40 | 1.50 | 1.30 | 132000 | 12 | 0.2 | 0.4 | -0.3 |
这就是我现在要回来的东西
| fixture_id | H_odds | D_odds | A_odds | ev_tstamp | updated | dif_h | dif_d | dif_a |
|---|---|---|---|---|---|---|---|---|
| 120000 | 1.40 | 1.50 | 1.30 | 132000 | 12 | 0.2 | 1.5 | -0.3 |
我正在使用的代码
select
t_max.*,
(t_max.H_odds - t_min.H_odds) as dif_h,
(t_max.D_odds - t_min.D_odds) as dif_d,
(t_max.A_odds - t_min.A_odds) as dif_a
from
(
select
fixture_id,
min(updated) min_updated,
max(updated) max_updated
from
test
group by
fixture_id
) as t1
join test as t_min on (t_min.fixture_id = t1.fixture_id and t_min.updated = t1.min_updated)
join test as t_max on (t_max.fixture_id = t1.fixture_id and t_max.updated = t1.max_updated)
【问题讨论】:
-
MIN(CASE WHEN D_odds > 0 THEN updated END)。如果该行中的值为0,那么它将被 MIN() 忽略。 -
那么您想要最新赔率,以及相同赛程和结果的最新赔率和最早赔率之间的差异?
-
是的,我希望在计算差异时跳过 0 赔率,因此每个赔率列必须单独处理,如下面的答案中所述。如果您将在上面示例中的表列 dif_d 中,您可以看到是什么意思