2017-03-20 39 views
1

我已經傳遞了兩次參數(:PRODUCT_ID)。我怎麼能傳遞:PRODUCT_ID只有一次低於查詢如何傳遞以下SQL查詢的單個參數

select count(1) 
from (
    select count(1) album_fa_counter 
    from actual_configs ac 
    where ac.config_id = :PRODUCT_ID 
     and exists (
      select 1 
      from config_participants cp 
      where CONTRIBUTOR_CATEGORY = 'Featured Artist' 
       and cp.gpid = ac.gpid 
      ) 
    ) a, 
    (
     select count(1) matching_track_fa_counter 
     from actual_tracks at1, 
      actual_configs ac1 
     where at1.gpid = ac1.gpid 
      and ac1.config_id = :PRODUCT_ID 
      and exists (
       select 1 
       from recording_participants rp, 
        config_participants cp 
       where cp.CONTRIBUTOR_CATEGORY = 'Featured Artist' 
        and cp.gpid = at1.gpid 
        and cp.participant_name = rp.participant_name 
        and rp.CONTRIBUTOR_CATEGORY = 'Featured Artist' 
        and rp.isrc = at1.isrc 
       ) 
     ) b 
where a.album_fa_counter = 0 
    or b.matching_track_fa_counter > 0; 

回答

0

沒有太多修改你的代碼,一個簡單的方法可能是通過使用CTE來包含變量,然後用它在您的查詢中加入:

with yourVariable(val) as (select :PRODUCT_ID from dual) 
select count(1) 
from (
    select count(1) album_fa_counter 
    from actual_configs ac, 
     yourVariable 
    where ac.config_id = yourVariable.val 
     and exists (
      select 1 
      from config_participants cp 
      where CONTRIBUTOR_CATEGORY = 'Featured Artist' 
       and cp.gpid = ac.gpid 
      ) 
    ) a, 
    (
     select count(1) matching_track_fa_counter 
     from actual_tracks at1, 
      actual_configs ac1, 
      yourVariable 
     where at1.gpid = ac1.gpid 
      and ac1.config_id = yourVariable.val 
      and exists (
       select 1 
       from recording_participants rp, 
        config_participants cp 
       where cp.CONTRIBUTOR_CATEGORY = 'Featured Artist' 
        and cp.gpid = at1.gpid 
        and cp.participant_name = rp.participant_name 
        and rp.CONTRIBUTOR_CATEGORY = 'Featured Artist' 
        and rp.isrc = at1.isrc 
       ) 
     ) b 
where a.album_fa_counter = 0 
    or b.matching_track_fa_counter > 0; 

順便說一句,我編輯你的代碼而不用重寫它,但是在ANSI SQL中做更多的努力並重寫它會更好。

+0

此查詢正在爲我工​​作..謝謝 – Madhava

0

我會嘗試解決這個問題這樣的:

With 
a as 
(select count(1) album_fa_counter 
from actual_configs ac 
where ac.config_id = :PRODUCT_ID 
    and exists (
     select 1 
     from config_participants cp 
     where CONTRIBUTOR_CATEGORY = 'Featured Artist' 
      and cp.gpid = ac.gpid), 

b as 
(
    select count(1) matching_track_fa_counter 
    from actual_tracks at1, 
     actual_configs ac1, 
     a 
    where at1.gpid = ac1.gpid 
     and ac1.config_id = ac.config_id 
     and exists (
      select 1 
      from recording_participants rp, 
       config_participants cp 
      where cp.CONTRIBUTOR_CATEGORY = 'Featured Artist' 
       and cp.gpid = at1.gpid 
       and cp.participant_name = rp.participant_name 
       and rp.CONTRIBUTOR_CATEGORY = 'Featured Artist' 
       and rp.isrc = at1.isrc 
      ) 
Select count (1) 
from a,b 
Where 
a.album_fa_counter = 0 
or b.matching_track_fa_counter > 0; 

現在數據庫中看到A和B作爲真實的表,讓你在使用的參數a,應該被看作是表中正常的可用值。 希望這對你有用。