我有兩張桌子:
POSITION_TABLE
| 帳戶 | 安全 | Pos_Quantity |
|---|---|---|
| 1 | 一個 | 100 |
| 2 | 乙 | 200 |
貿易表
| 帳戶 | 安全 | 貿易_數量 |
|---|---|---|
| 1 | 一個 | 50 |
| 2 | C | 10 |
我想以一種將匹配的行顯示為一行的方式加入它們,但也會顯示不匹配的行,因此標準的 LEFT JOIN 不起作用。
預期輸出:
| 帳戶 | 安全 | Pos_Quantity | 貿易_數量 |
|---|---|---|---|
| 1 | 一個 | 100 | 50 |
| 2 | 乙 | 200 | 0 |
| 2 | C | 0 | 10 |
我怎么做?
uj5u.com熱心網友回復:
完整的外部連接在這里可以很好地作業:
with position_table as (select 1 account, 'A' security, 100 pos_quantity from dual union all
select 2 account, 'B' security, 200 pos_quantity from dual),
trade_table as (select 1 account, 'A' security, 50 trade_quantity from dual union all
select 2 account, 'C' security, 10 trade_quantity from dual)
select coalesce(pt.account, tt.account) account,
coalesce(pt.security, tt.security) security,
coalesce(pt.pos_quantity, 0) pos_quantity,
coalesce(tt.trade_quantity, 0) trade_quantity
from position_table pt
full outer join trade_table tt on pt.account = tt.account
and pt.security = tt.security
order by account,
security;
db<>fiddle - 請注意您如何看到完整的外部連接與 where 子句中定義的子查詢一起作業得很好!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/484741.html
