2017-04-05 13 views
0

次のクエリを1つのクエリに結合できますか?以下のクエリを1つのクエリとして結合することは可能ですか

select a.usr_id, a.usr_desc as ,a.usr_type,b.usr_desc as usr_prof_name 
from sc_usr_prof_m a, sc_usr_type_m b 
where a.usr_type=b.usr_type 
and a.current_status='A' 
order by a.usr_id; 
select distinct a.usr_id,a.usr_desc,b.dept_name 
from sc_usr_prof_m a ,sc_user_depts b 
where a.usr_id=b.usr_id 
and a.usr_status='E' 
and a.current_status='A'; 
select a.usr_id,a.usr_desc,b.curr_code,b.min_amt,b.max_amt 
from sc_usr_prof_m a, sc_auth_limit_m b 
where a.usr_id=b.usr_id 
and a.usr_status='E' 
and a.current_status='A' 
and b.max_amt not in ('0') 
order by b.usr_id; 
select a.usr_id,a.usr_desc,b.msg_type 
from sc_usr_prof_m a, sc_user_msgs_m b 
where a.usr_id=b.usr_id 
and a.usr_status='E' 
and a.current_status='A' 
and b.swift_enable='Y' 
order by a.usr_id; 
+0

ようこそ、私はあなたの質問を書式設定し、タイトルに基づいて身体に簡単な質問を追加しましたが、あなたの実際の投稿に多くのコンテンツを追加する必要があります。また、テーブル構造の例、いくつかのサンプルデータ、および結果データセットがどのように見えるかのサンプルを示した場合に役立ちます。 [How to Ask](http://stackoverflow.com/help/how-to-ask)セクションを読んでください。 – gmiley

+0

実行可能、はい。賢明ではないでしょう。最初のクエリは 'sc_usr_prof_m'から別のレコードセットをフィルタリングするので、他のクエリと組み合わせると結果セットが変更されます。それぞれのクエリは異なるテーブルに結合します。おそらく 'usr_id'以外の共通の結合カラムがないので、デカルト積を生成します。 – APC

答えて

0

KNOなしあなたのテーブル構造を羽ばたくと、あなたに最高の結果をもたらす方法を正確に言うのは難しいでしょう。 は、さまざまなテーブルを一緒に結合するクエリを1つに書き直すことができますが、選択しているテーブルの種類がかなり異なっているようです。あなたのテーブルデザインを知らなければ、それが行動の最善のコースであるかどうかを言うことは難しいだろう。

また、結果セットを単一のデータセットに結合する場合は、基本的に各クエリの結果を単一の出力にマージすると、UNION [ALL]を使用できます。各照会の選択リストの列が正しく一致/整列されていることを確認する必要があります。これは理想的ではありませんが、あなたが望むものを与えるはずです。

with combined_results as (
    select a.usr_id as usr_id, 
     a.usr_desc as usr_desc, 
     a.usr_type as usr_type, 
     b.usr_desc as usr_prof_name, 
     null as dept_name, 
     null as cur_code, 
     0 as min_amt, 
     0 as max_amt, 
     null as msg_type 
    from sc_usr_prof_m a, sc_usr_type_m b 
    where a.usr_type=b.usr_type 
    and a.current_status='A' 
    union all 
    select distinct 
     a.usr_id as usr_id, 
     a.usr_desc as usr_desc, 
     null as usr_type, 
     null as usr_prof_name, 
     b.dept_name as dept_name, 
     null as cur_code, 
     0 as min_amt, 
     0 as max_amt, 
     null as msg_type 
    from sc_usr_prof_m a ,sc_user_depts b 
    where a.usr_id=b.usr_id 
    and a.usr_status='E' 
    and a.current_status='A' 
    union all 
    select a.usr_id as usr_id, 
     a.usr_desc as usr_desc, 
     null as usr_type, 
     null as usr_prof_name, 
     null as dept_name, 
     b.curr_code as curr_code, 
     b.min_amt as min_amt, 
     b.max_amt as max_amt, 
     null as msg_type 
    from sc_usr_prof_m a, sc_auth_limit_m b 
    where a.usr_id=b.usr_id 
    and a.usr_status='E' 
    and a.current_status='A' 
    and b.max_amt not in ('0') 
    union all 
    select a.usr_id as usr_id, 
     a.usr_desc as usr_desc, 
     null as usr_type, 
     null as usr_prof_name, 
     null as dept_name, 
     null as cur_code, 
     0 as min_amt, 
     0 as max_amt, 
     b.msg_type as msg_type 
    from sc_usr_prof_m a, sc_user_msgs_m b 
    where a.usr_id=b.usr_id 
    and a.usr_status='E' 
    and a.current_status='A' 
    and b.swift_enable='Y' 
) 
select * 
from combined_results 
order by usr_id; 

私はあなたの加入書き出すことも示唆している:

select a.col1, a.col2, b.col3, b.col4 
from my_a_table a, my_b_table b 
where b.col1 = a.col1 
and a.col1 = 123; 

をした場合、読みやすさを超えない他の理由のために、特に:代わりに、あなたは現在、それをやっているかの

select a.col1, a.col2, b.col3, b.col4 
from my_a_table a 
join my_b_table b 
on b.col1 = a.col1 
where a.col1 = 123; 

を複数の結合を持つ大規模なクエリでは

+0

私を助けてください – SMILY

関連する問題