【问题标题】:Oracle query optimisation without sub-query无子查询的Oracle查询优化
【发布时间】:2020-05-28 13:04:34
【问题描述】:

我希望使我的查询更加优化和简单。 我只需要获取那些得分最高的 T_ID,如果那个 T_ID 有 Msg 'C' 那么它不应该被包括在内。

我的数据如下:

我的查询是:

SELECT DISTINCT T_ID, Msg, MAX(Score) over (Partition by T_ID)
FROM Sample
WHERE T_ID NOT IN (SELECT T_ID FROM Sample WHERE Msg = 'C');

我的答案是:

有没有办法通过只调用一次数据库来实现这一点。

【问题讨论】:

    标签: oracle performance optimization oracle-sqldeveloper


    【解决方案1】:

    提示 #1:图像不利于用您的数据构建测试用例 :-(

    但是你可以做这样的事情,假设“0”是一个合适的边界(或者可能是-1)

    SQL> with t as (
      2    select 77 t_id, 'N' msg, 220 score from dual union all
      3    select 77 t_id, 'C' msg, 220 score from dual union all
      4    select  1 t_id, 'N' msg, 120 score from dual union all
      5    select  1 t_id, 'N' msg, 102 score from dual union all
      6    select  2 t_id, 'N' msg, 112 score from dual union all
      7    select  2 t_id, 'C' msg, 152 score from dual union all
      8    select  3 t_id, 'N' msg, 172 score from dual union all
      9    select  3 t_id, 'N' msg, 192 score from dual union all
     10    select  4 t_id, 'N' msg, 100 score from dual union all
     11    select  5 t_id, 'N' msg, 101 score from dual
     12  )
     13  select t_id, max(score)
     14  from t
     15  group by t_id
     16  having min(decode(msg,'C',0,score)) > 0;
    
          T_ID MAX(SCORE)
    ---------- ----------
             1        120
             4        100
             5        101
             3        192
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多