0

3つのテーブルから値を選択したい。各テーブルにはbuyer_entity_id列があります。

2つのテーブルで外部結合を行うための構文を思いついたのですが、3番目のテーブルを追加する方法がわかりません。

これが、2つのテーブル結合のステートメントです。これは、私が望むとおりに機能します。

select * from (select  b.buyer_entity_id, count(distinct(a.row_id)) as imps 
from imps a, anl_line b
where b.line_item_id=a.buyer_line_id 
and a.entity_id=3
group by b.buyer_entity_id
order by b.buyer_entity_id) tab1
full outer join (select b.buyer_entity_id, count(distinct(a.row_id)) as clicks 
from clicks a, anl_line b 
where a.buyer_line_id=b.line_item_id 
and a.entity_id=3
group by b.buyer_entity_id 
order by b.buyer_entity_id) tab2
on tab1.buyer_entity_id = tab2.buyer_entity_id;

3番目のテーブルには同じselectステートメントがあり、buyer_entity_id値にも結合されます。ただし、3番目のselectステートメントを追加すると、「キーワードがありません」というエラーが発生します。以下は、私の3方向の完全な外部結合ステートメントです。

select * from ((select  b.buyer_entity_id, count(distinct(a.row_id)) as imps 
from imps_table a, line_table b
where b.line_item_id=a.buyer_line_id 
and a.entity_id=3
group by b.buyer_entity_id
order by b.buyer_entity_id) tab1
full outer join (select b.buyer_entity_id, count(distinct(a.row_id)) as clicks 
from clicks_table a, line_table b 
where a.buyer_line_id=b.line_item_id 
and a.entity_id=3
group by b.buyer_entity_id 
order by b.buyer_entity_id) tab2)
outer join (select  b.buyer_entity_id, count(distinct(a.row_id)) as vers 
from vers_table a, line_table b
where b.line_item_id=a.buyer_line_id 
and a.entity_id=3
group by b.buyer_entity_id
order by  b.buyer_entity_id) tab3
on tab1.buyer_entity_id = tab2.buyer_entity_id and tab2.buyer_entity_id=tab3.buyer_entity_id;
4

3 に答える 3

1

WITH句を使用すると、少し単純化できます。

first_partを((select ....)外部結合(select ....));として使用します。select * from first_part external join(select ....);

ただし、全体的なロジックを再検討して、実行しようとしていることを達成するためのより良い方法があるかどうかを確認することをお勧めします。多くの場合、複数の外部結合が最も効率的なソリューションではありません。

于 2011-11-04T19:49:53.880 に答える
1

エラーはここにあります:order by b.buyer_entity_id) tab2)ontab1とtab2の間の結合条件を指定する句が必要です。order by b.buyer_entity) tab1 on <join condition)

それが修正されたら。full次の行に追加して、がにouter join (select b.buyer_entity_id, count(distinct(a.row_id)) as versなるようにする必要がありますfull outer join (select b.buyer_entity_id, count(distinct(a.row_id)) as vers

これは構文の問題をカバーしますが、セマンティックの問題はカバーしません。

結合はペアワイズテーブル1が何らかの条件でテーブル2に結合され、その結果が次の結合の左側になります。tab1とtab3が一致するが、tab2が一致しない場合、どうしますか?tab1とtab3のデータとtab2のnullを含む行を生成すると思います。何かのようなもの:

select * 
from (select  b.buyer_entity_id, count(distinct(a.row_id)) as imps 
        from imps_table a, line_table b
        where b.line_item_id=a.buyer_line_id 
        and a.entity_id=3
        group by b.buyer_entity_id) tab1
full outer join (select b.buyer_entity_id, count(distinct(a.row_id)) as clicks 
        from clicks_table a, line_table b 
        where a.buyer_line_id=b.line_item_id 
        and a.entity_id=3
        group by b.buyer_entity_id) tab2
    on tab1.buyer_entity_id = tab2.buyer_entity_id
full outer join (select  b.buyer_entity_id, count(distinct(a.row_id)) as vers 
        from vers_table a, line_table b
        where b.line_item_id=a.buyer_line_id 
        and a.entity_id=3
        group by b.buyer_entity_id) tab3
    on tab3.buyer_entity_id in (tab1.buyer_entity_id, tab2.buyer_entity_id)
order by coalesce(tab1.buyer_entity_id
    , tab2.buyer_entity_id
    , tab3.buyer_entity_id);

order byまた、サブクエリで負けてください。注文が参加を生き残ることが保証されていないため、彼らはあなたのために何もしていません。

于 2011-11-04T20:21:12.233 に答える
1

より高速な方法は、ここに記載されているとおりです https://forums.oracle.com/thread/2388229

SELECT       COALESCE (a.id, b.id, c.id)     AS common_id
,       NVL2 (a.id, 1, NULL)           AS table_a_flag
,       NVL2 (b.id, 1, NULL)           AS table_b_flag
,       NVL2 (c.id, 1, NULL)           AS table_c_flag
FROM              table_a  a
FULL OUTER JOIN  table_b  b  ON  b.id  =           a.id
FULL OUTER JOIN      table_c  c  ON  c.id  = COALESCE (a.id, b.id)
ORDER BY  common_id;
于 2013-06-17T09:30:23.383 に答える