Sql 有点像右连接

Sql 有点像右连接,sql,sql-server,outer-join,Sql,Sql Server,Outer Join,我在SQL server中有以下两个SQL表: Payment(physical payment made) +----+-------+-----------+---------------------+ | id |Amount | BookingId | Transaction Type | +----+-------+-----------+---------------------+ | 1 | 10000 | 2 | p(normal payment) |

我在SQL server中有以下两个SQL表:

Payment(physical payment made)
+----+-------+-----------+---------------------+
| id |Amount | BookingId | Transaction Type    |
+----+-------+-----------+---------------------+
| 1  | 10000 |   2       |   p(normal payment) |
+----+-------+-----------+---------------------+
| 2  | 90000 |   2       |   p(normal payment) |
+----+-------+-----------+---------------------+
| 3  | 8000  |   2       |   p(normal payment) |
+----+-------+-----------+---------------------+
| 4  | 8000  |   3       |   r(refunds)        |
+----+-------+-----------+---------------------+
| 5  | 10000|    4       |   r(refunds)        |
+----+-------+-----------+---------------------+
未来将要进行的退款

+----+--------+-----------+
| id | Amount | BookingId |
+----+--------+-----------+
| 1  | 10000  |   2       |
+----+--------+-----------+
第一个表包含不同的付款类型正常付款、退款、根据bookingid实际支付的代币付款

第二个表格包含未来将为特定BookingID退款的记录

我正在从第一个表中提取所有退款,但我也想在一个数据集中提取BookingID2

结果

+----+-------+-----------+------------+
|id  |Amount | BookingId | Difference |
+----+-------+-----------+------------+
| 4  | 8000  |    3      |  0         |
+----+-------+-----------+------------+
| 5  | 10000 |    4      |  0         |
+----+-------+-----------+------------+
| 6  |   0   |    2      |  10000     |
+----+-------+-----------+------------+
任何帮助都将不胜感激。3号和4号预订有退款,但2号预订没有退款

SELECT id, Amount, BookingId, 0 AS Difference FROM FIrstTable WHERE Transaction Type = 'R'
UNION ALL
SELECT id, 0 AS Amount, BookingId, Amount AS Difference FROM SecondTable