用每行两个互补值查询

穆纳·奇克纳(Mouna Cheikhna)

我在非常大的查询中有一个子查询,必须执行以下操作

有几对集群

数组(数组(c1,c2),数组(c3,c4),数组(c5,c6),数组(c7,c8))

例如c1和c2是互补的,c3和c4也是..etc。而且我有一个表状态:

id_state cluster   successfull   failed  success_ratio   
  1        c1           4            0       100%  
  2        c2           1            9       10%   
  3        c3           0            4        0%         
  4        c4           1            1        50% 

请注意,使用上面的数组确定了哪个群集与另一个群集耦合。

和我想要的最终输出:

   cluster  successfull success_ratio                        
       c1         4        100%    (for the first pair)
       c4         1        50%      (for the second)

有没有一种方法可以通过仅从每对夫妇中获取成功率> 50%的数据并且只有当两者的成功率<50%时才获取第一个数据来获取所有数据的成功率的查询。

仅使用mysql查询甚至可以实现(我不能使用查询结果,因为我希望它作为另一个大型查询的子查询)?

即使您可以提出一些建议的起点,也应赞赏。

戈登·利诺夫(Gordon Linoff)

听起来您只想要每对的最大成功率。

select s.grp, max(success_ratio)
from state s join
     (select 'c1' as cluster, 1 as grp union all
      select 'c2', 1 union all
      select 'c3', 2 union all
      select 'c4', 2 union all
      select 'c5', 3 union all
      select 'c6', 3 union all
      select 'c7', 4 union all
      select 'c8', 4
     ) grps
     on s.cluster = grps.cluster
group by s.grp;

如果您实际上希望行取得最大的成功,请使用子查询:

select s.*
from (select s.grp,
      substring_index(group_concat(cluster order by success_ratio desc), ',', 1) as bestcluster
      from state s join
           (select 'c1' as cluster, 1 as grp union all
            select 'c2', 1 union all
            select 'c3', 2 union all
            select 'c4', 2 union all
            select 'c5', 3 union all
            select 'c6', 3 union all
            select 'c7', 4 union all
            select 'c8', 4
           ) grps
           on s.cluster = grps.cluster
      group by s.grp
     ) b join
     state s
     on s.cluster = b.cluster

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章