簡體   English   中英

Oracle 查詢優化無子查詢

[英]Oracle query optimisation without sub-query

我希望使我的查詢更加優化和簡單。 我只需要獲取那些得分最高的 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');

我的回答是:

在此處輸入圖像描述

有沒有辦法通過只調用一次數據庫來實現這一點。

提示 #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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM