仅当高于 0 时才计算每列的 min 和 max 之间的差异 [英] Calculate difference between min and max for each column only if higher then 0

查看:83
本文介绍了仅当高于 0 时才计算每列的 min 和 max 之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据更新"列中的值计算赔率之间的差异,此时我将更新值为最小值的赔率从更新值为最大值的赔率中减去.它工作完美,但我刚刚意识到在某些列中有时恰好为 0,我想知道是否可以根据更新的列和仅高于 0 的值来选择最小值.

I need to calculate the difference between odds based on the value in the 'updated' column at the moment I take odds where the updated value is a min and minus it from odds where the updated value is max. It works perfect but I've just realized that in some columns happens to be 0 sometimes and I was wondering if it's possible to select the minimum still based on the updated column and only values where higher than 0.

这就是桌子的样子

<头>
fixture_idH_oddsD_oddsA_oddsev_tstamp更新
1200001.401.501.3013200012
1200001.101.101.1013200011
1200001.2001.6013200010

这就是我想要回来的

<头>
fixture_idH_oddsD_oddsA_oddsev_tstamp更新dif_hdif_ddif_a
1200001.401.501.30132000120.20.4-0.3

这就是我现在要回来的

<头>
fixture_idH_oddsD_oddsA_oddsev_tstamp更新dif_hdif_ddif_a
1200001.401.501.30132000120.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)

推荐答案

我只是稍微修改了代码以计算仅针对特定赔率组 (avg) 的差异,因此看起来如下所示.但是它只工作了一次,处理时间超过 15 秒,其他时候我尝试过它由于超时错误而不起作用.只是为了在我的结构中澄清市场列是您示例中的结果"列.

I just modify the code little bit to calculate the difference only for specific group of odds (avg) so it look like bellow. It worked just once though, it took over 15 seconds to process and the other times I tried it didn't work due to time out error. Just to clarify in my structure the market column is the 'outcome' column from your example.

explain SELECT a.*
    , a.odds - b.odds delta
FROM 
 ( SELECT x.*
     FROM average_odds x
     JOIN
        ( SELECT fix_id
               , market
               , MAX(updated) min_updated
            FROM average_odds x where odds_type=avg
           GROUP BY fix_id
               , market
        ) y
       ON y.fix_id = x.fix_id
      AND y.market = x.market
      AND y.min_updated = x.updated
 ) a
JOIN
( SELECT x.*
     FROM average_odds x
     JOIN
        ( SELECT fix_id
               , market
               , MIN(updated) min_updated
            FROM average_odds x where odds_type=avg
           GROUP BY fix_id
               , market
        ) y
       ON y.fix_id = x.fix_id
      AND y.market = x.market
      AND y.min_updated = x.updated
) b
 ON b.fix_id = a.fix_id
AND b.market = a.market  
ORDER BY `delta` ASC

这是解释表

<头>
IDS TYPE表格..parti输入pos_keysKEYkey lenref过滤额外
1PRIMARY衍生3>null全部nullnullnullnull17466100.00使用临时的;使用文件排序
1PRIMARYxnullref修复、修复、市场、更新修复4y.fix_id5960.11使用 where
1PRIMARYxnullref修复、修复、市场、更新修复4y.fix_id5962.27使用 where
1PRIMARY衍生5>nullrefauto_key0>auto_key0>31y.fix_id,y.market,bobi.x.updated10100.00使用索引
5派生xnullrefbokiboki4const17466100.00使用索引条件;使用临时;使用文件...
3派生xnullrefbokiboki4const17466100.00使用索引条件;使用临时;使用文件...

这篇关于仅当高于 0 时才计算每列的 min 和 max 之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆