2011-06-01 35 views
1

我想知道是否有可能在相同的查詢中加入相同的子選擇而不必再次執行查詢?下面的查詢是實際模糊查詢我想運行SQL:是否可以加入相同的子選擇?

select * from 

     (

     -- Sub query A - same as sub query B 
      select bc.service_type, bc.cid, min(bc.last_modified_date) as last_modified1 from 
       (

       select * from table_a bc2 
       where bc2.state != 7 
       AND bc2.cid in 
       ( 

        select cid from table_a TA, table_b TB 
        where TB.name not like '% IS' and TA.state != 7 
        AND TA.service_type = 1 
        AND TA.username is not null 
        and TA.bctid = TB.bctid 
       ) 
      ) bc 
      group by service_type, cid 
     ) result1, 

     (

     // Sub query B - same as sub query A 
      select bc.service_type, bc.cid, min(bc.last_modified_date) as last_modified2 from 
       (

       select * from table_a bc2 
       where bc2.state != 7 
       AND bc2.cid in 
       ( 

        -- select affected records 
        select cid from table_a TA, table_b TB 
        where TB.name not like '% IS' and TA.state != 7 
        AND TA.service_type = 1 
        AND TA.username is not null 
        and TA.bctid = TB.bctid 
       ) 
      ) bc 
      group by service_type, cid 
     ) result2 

where result1.service_type = 1 
and result2.service_type = 2 
and result1.cid = result2.cid 
and result1.last_modified1 < result2.last_modified2 

的重複子查詢的解釋計劃昂貴給出表的大小,所以我非常不希望兩次運行它。我正在尋找的是克隆第一個查詢的結果並將其加入自身的一些方法!

這是要在Oracle中運行的情況下,有一些數據庫特定的擴展名。

回答

10

使用with語句:

with bar as (
select * from foo where ... 
) 
select bar.* from bar join bar barian on ... 
+0

完美的感謝,我不知道那一個! – Ellis 2011-06-01 14:21:13

+1

@Ellis:這些稱爲Common Table Expressions或簡稱CTE。 – 2011-06-01 19:35:01

0

丹尼斯,解決方案是,在甲骨文的最好方式。

但是,如果您需要在多個查詢中使用該子選擇,則可能需要創建一個視圖。

+0

要完成此圖:「WITH」查詢(也稱爲「公用表表達式」)實際上不是Oracle特定的。它是在SQL標準中定義的,幾乎可用於所有主要的DBMS – 2011-06-01 14:20:27

+0

這是一次性查詢,因此在這種情況下要避免創建視圖。 – Ellis 2011-06-01 14:24:17

+0

@a_horse_with_no_name:幾乎所有......但不是SQLite這是一個廣泛使用的數據庫管理系統雖然(雖然不是基於客戶端服務器的) – Benoit 2011-06-01 14:28:56

0

我有原來的查詢可以改寫成一種感覺(我沒有測試這一點):

select * from 
    (
     select bc2.cid, min(bc2.last_modified_date) as last_modified1 from table_a bc2 
     where bc2.state != 7 
     AND bc2.cid in 
     ( 
      select cid from table_a TA, table_b TB 
      where TB.name not like '% IS' and TA.state != 7 
      AND TA.service_type = 1 
      AND TA.username is not null 
      and TA.bctid = TB.bctid 
     ) 
     and bc2.service_type = 1 
     group by cid 
    ) result1, 
    ( 
     select bc2.cid, min(bc2.last_modified_date) as last_modified1 from table_a bc2 
     where bc2.state != 7 
     AND bc2.cid in 
     ( 
      select cid from table_a TA, table_b TB 
      where TB.name not like '% IS' and TA.state != 7 
      AND TA.service_type = 1 
      AND TA.username is not null 
      and TA.bctid = TB.bctid 
     ) 
     and bc2.service_type = 2 
     group by cid 
    ) result2 
    where result1.cid = result2.cid 
    and result1.last_modified1 < result2.last_modified2 

我想你可以推升謂詞「裏result1.service_type = 1 和RESULT2 .service_type = 2「。之後,你可以做Denis建議的事情。

相關問題