Объедините два запроса в Oracle и получите данные в oracle - PullRequest
0 голосов
/ 10 июля 2020

У меня есть два запроса, которые приводят к данным из каждой таблицы

Запрос 1

enter image description here

Query 2

enter image description here

I need to combine these Queries and get the data as below as we see Account ID (100) do exist the same from both queries take row for that Account ID from first Query.

введите описание изображения здесь

Любая помощь приветствуется.

Ответы [ 2 ]

1 голос
/ 10 июля 2020

Для SQL Server и Oracle вы можете использовать такую ​​конструкцию ....

Select Address_ID, Display_Value, Account_ID
from FirstTable
union
Select Address_ID, Display_Value, Account_ID
from SecondTable
Where SecondTable.Account_ID not in (select FirstTable.Account_ID from FirstTable);
0 голосов
/ 10 июля 2020

В качестве альтернативы может помочь полное внешнее соединение.

SQL> with
  2  -- sample data
  3  t1 (address_id, display_value, account_id) as
  4    (select 1000, '10001 - Test 1', 100 from dual union all
  5     select 1000, '10002 - Test 2', 200 from dual union all
  6     select 1000, '10003 - Test 3', 300 from dual
  7    ),
  8  t2 (address_id, display_value, account_id) as
  9    (select 2000, '10001 - Test 1', 100 from dual union all
 10     select 3000, '10004 - Test 4', 400 from dual
 11    )
 12  --
 13  select nvl(a.address_id, b.address_id) address_id,
 14         nvl(a.display_value, b.display_value) display_value,
 15         nvl(a.account_id, b.account_id) account_id
 16  from t1 a full outer join t2 b on a.account_id = b.account_id;

ADDRESS_ID DISPLAY_VALUE  ACCOUNT_ID
---------- -------------- ----------
      1000 10001 - Test 1        100
      1000 10002 - Test 2        200
      1000 10003 - Test 3        300
      3000 10004 - Test 4        400

SQL>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...