【发布时间】:2021-11-07 06:14:03
【问题描述】:
我有两个表,我正在尝试将它们合并在一起以创建它们的联合输出。
这是我的表格的结构:
表 1:
| date | type_s | calls | declines |
|---|---|---|---|
| 09-SEP-21 | insurance | 500 | 600 |
| 09-SEP-21 | roadside | 66 | 60 |
| 09-SEP-21 | AAA | 34 | 700 |
| 09-SEP-21 | retail | 1 | 650 |
表 2:
| date | type_s | cnt |
|---|---|---|
| 09-SEP-21 | insurance | 5 |
| 09-SEP-21 | AAA | 3 |
| 09-SEP-21 | retail | 79 |
如何让我的输出变成这样:
| date | type_s | calls | declines | cnt |
|---|---|---|---|---|
| 09-SEP-21 | insurance | 500 | 600 | 5 |
| 09-SEP-21 | roadside | 66 | 60 | 0 |
| 09-SEP-21 | AAA | 34 | 700 | 3 |
| 09-SEP-21 | retail | 1 | 650 | 79 |
请注意,在表 2 中,路边不存在,因为没有该类型的数据。我正在尝试将它们合并在一起以保留所有记录,即使表 1 中的列 'type_s' 中的数据在表 2 中不存在。在这种情况下,只需将 0 作为该类型的 cnt。
到目前为止,这是我的代码:
with t2 as
(select DATE_TIME , type_s, calls, declines
from table_1
),
CD as
(
SELECT
DATE_TIME, type_s, CNT
FROM table2
),
DD AS(
SELECT TO_DATE(current_date - 1) AS chart_date,
type_s,
calls,
declines
FROM t2),
GG As
(
SELECT
DD.chart_date ,
DD.type_s,
DD.calls,
DD.declines,
CD.CNT
FROM CD,DD
WHERE CD.type_s= DD.type_s)
select CHART_DATE,type_s,calls,declines,cnt
from GG;
我的代码运行良好,但它没有给我想要的输出(见下文)。它不包括表 1 中的路边行。
| date | type_s | calls | declines | cnt |
|---|---|---|---|---|
| 09-SEP-21 | insurance | 500 | 600 | 5 |
| 09-SEP-21 | AAA | 34 | 700 | 3 |
| 09-SEP-21 | retail | 1 | 650 | 79 |
关于如何修复我的代码以创建我想要的输出的任何想法或建议?任何帮助将不胜感激。
【问题讨论】: