dpgui8229808 2014-01-17 15:29
浏览 48
已采纳

查询每行有两个互补值

I have a subquery in a very large query that has to do the following

with an array of couples of clusters

array(array(c1, c2), array(c3, c4), array(c5, c6), array(c7, c8))

where for example c1 and c2 are complementary, c3 and c4 as well ..etc . and i have a table state :

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% 

note that which cluster is coupled with the another is determined using the array above.

and the final output that i would like to have :

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

is there a way to do a query that gets the success_ratio of all the data by taking only from each couple the one with the success_ratio > 50% and only if both have success_ratio < 50% then just take the first one.

Is this even achievable using only a mysql query (i can't use query result since i want it as a subquery of another large query) ?

even if you can just suggest a starting point for a way to do it that would be appreciated.

  • 写回答

1条回答 默认 最新

  • dougao7801 2014-01-17 15:36
    关注

    It sounds like you just want the maximum success ratio for each pair.

    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;
    

    If you actually want the rows with the best success, then use a subquery:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wpf datagrid如何实现多层表头
  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)