2016-08-04 87 views
0

我創建了一個Oracle SQL查詢,該查詢鏈接到我在Oracle的一個Oracle FROM子句中使用的大約五個表,但查詢的問題是某些記錄是重複的,所以我只想在表單中顯示一行而不是任何重複的記錄。我已經嘗試了GROUP BY和PARTITION BY語句,但是查詢變慢並將其添加到語句中。Oracle SQL不會將重複項複製到Oracle表單10g

我現在正在考慮將此作爲一個過程來處理,如果發生任何重複事件,我們將只返回其中一個。將數據庫中的ORACLE記錄錶帶回表單最好嗎?如何最好在Oracle PL/SQL循環中查找重複內容?

我已經更新了這個問題,並加入了下面的完整查詢來更好地解釋它。下面select語句中的第一列surr_id是唯一的,但我想在Oracle窗體中顯示的是生產編號以及其他不唯一的列。可能有重複的產品編號,有時甚至有三個產品編號記錄相同。希望這可以幫助。我正在考慮把它放在一個循環中,只抓取第一個生產編號,然後在生產編號發生變化時纔將每個記錄帶回來。

select x.surr_id , 
    x.supplier_name as supplier , 
    x.broadcaster_name as broadcaster , 
    ptle.title as production_title , 
    x.production_number as production_number , 
    stle.title as series_title , 
    x.production_source as supplied_source_ind , 
    x.third_party_group_id , 
    x.bro_broadcast_by_tp_surr_id , 
    x.station_id from (select usage_headers.surr_id as surr_id , 
          broad_supp.supplier_name as supplier_name , 
          broad_supp.broadcaster_name as broadcaster_name , 
          usage_headers.production_number as production_number , 
          productions.production_source as production_source , 
          broad_supp.station_id as station_id , 
          usage_headers.prod_exploitation_cre_surr_id as prod_exploitation_cre_surr_id , 
          usage_headers.bro_broadcast_by_tp_surr_id as bro_broadcast_by_tp_surr_id , 
          productions.cre_surr_id as cre_surr_id , 
          productions.prod_series_cre_surr_id as prod_series_cre_surr_id , 
          broad_supp.third_party_group_id as third_party_group_id 
         from usage_headers, productions, (SELECT /*+ index (bro bro_pk) */ 
                   third_party.surr_id AS THIRD_PARTY_SURR_ID, 
                   third_party.supplier_group_id AS THIRD_PARTY_GROUP_ID, 
                   third_party.dn_root_tp_surr_id AS THIRD_PARTY_ROOT_ID, 
                   third_party.supplier_name, bro.station_id AS STATION_ID, 
                   bro.dn_tp_name AS BROADCASTER_NAME FROM (SELECT tp.surr_id, 
                               tp.name AS supplier_name, 
                               tp.tp_surr_id AS supplier_group_id, 
                               tp.dn_root_tp_surr_id FROM third_parties tp 
                               CONNECT BY PRIOR tp.surr_id = tp.tp_surr_id 
                               START WITH tp.surr_id IN (4251, 4247, 4237, 4034, 10157, 14362, 9834)) third_party 
         JOIN broadcasters bro ON (third_party.surr_id = bro.tp_surr_id)) broad_supp 
        where broad_supp.THIRD_PARTY_SURR_ID = usage_headers.bro_broadcast_by_tp_surr_id 
        AND usage_headers.prod_exploitation_cre_surr_id = productions.cre_surr_id 
        and usage_headers.prod_exploitation_cre_surr_id IS NOT NULL 
        and usage_headers.right_type in ('M','B') 
        AND usage_headers.udg_surr_id IS NOT NULL 
        AND NVL(usage_headers.dn_uls_usage_status,'3') NOT IN ('9', '11') 
        AND productions.production_source <> 'AP') x 
    LEFT OUTER JOIN titles ptle ON (ptle.cre_surr_id = x.cre_surr_id AND ptle.tt_code = 'R') 
    LEFT OUTER JOIN titles stle ON (stle.cre_surr_id = x.prod_series_cre_surr_id AND stle.tt_code = 'R') 

謝謝你們提前

+0

如果整個結果集行重複,則可以使用'distinct',但這可能表明您的查詢或數據存在問題...如果某些列重複但其他列是獨特的,則需要決定哪些列保持,然後可能聚合;但你似乎已經嘗試過了。分組不應該放慢速度,除非你目前只提取第一組行。 –

+0

您需要修復查詢,而不是用戶界面。 –

+0

嗨,夥計們,查詢中的數據是唯一的,但我想要帶回到表單中的數據不是,所以在查詢中選擇unique_id,prododuction,station但是以表單的形式,我只想顯示生產和工作站,但生產和車站有時重複。 –

回答

0

如果你得到完全是複製然後只需添加DISTINCT條款,所以你的選擇變得SELECT DISTINCT將確保只返回的記錄,一個記錄。如果即使一列不同,那麼這也不起作用。

+0

不同的是增加了語句的成本,而且這個DISTINCT減慢了查詢的速度,因爲它必須對錶進行全面掃描以獲得所有不同的值。 –

+0

在這種情況下,我們需要查看完整的聲明,以便我們可以防止重複出現在更早的時間點。 –

+0

@ShaunKinnair - 增加'distinct'不會強制執行全表掃描,但它會添加一個排序/修剪操作;如果您已經訂購了結果,我會很驚訝,如果它明顯較慢。如果你不是那麼它可能會慢一點,但似乎更可能是你目前只提取第一組行。 –