合并Oracle中的两个查询并获取oracle中的数据
Combine Two Queries in Oracle and get the data in oracle
我有两个查询结果低于每个 table
的数据
查询 1
查询 2
我需要组合这些查询并获取如下数据,因为我们看到帐户 ID (100) 确实存在,两个查询中的帐户 ID 都相同,从第一个查询中获取该帐户 ID 的行。
非常感谢任何帮助。
对于 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);
或者,完全外部联接可能会有所帮助。
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>
我有两个查询结果低于每个 table
的数据查询 1
查询 2
我需要组合这些查询并获取如下数据,因为我们看到帐户 ID (100) 确实存在,两个查询中的帐户 ID 都相同,从第一个查询中获取该帐户 ID 的行。
非常感谢任何帮助。
对于 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);
或者,完全外部联接可能会有所帮助。
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>