用另一个表中的列值替换列的null



我有来自两个表的数据流,表A和表B。我正在对两个表中的一个公共列进行内部联接,并根据不同的条件再创建两个新列。以下是一个示例数据集:

表A

| Id  | StartDate  |
|-----|------------|
| 119 | 01-01-2018 |
| 120 | 01-02-2019 |
| 121 | 03-05-2018 |
| 123 | 05-08-2021 |

表B

| Id  | CodeId | Code | RedemptionDate |
|-----|--------|------|----------------|
| 119 | 1      | abc  | null           |
| 119 | 2      | abc  | null           |
| 119 | 3      | def  | null           |
| 119 | 4      | def  | 2/3/2019       |  
| 120 | 5      | ghi  | 04/7/2018      |
| 120 | 6      | ghi  | 4/5/2018       |
| 121 | 7      | jkl  | null           |
| 121 | 8      | jkl  | 4/4/2019       |
| 121 | 9      | mno  | 3/18/2020      |
| 123 | 10     | pqr  | null           |

我基本上要做的是在StartDate>2018,并创建两个新列-当RedemptionDate为null时通过计数CodeId来"解锁",当RedmeptionDate不为null时,通过计数CodeId来"兑换"。下面是SQL查询:

WITH cte1 AS (
SELECT a.id, COUNT(b.CodeId) AS 'Unlock'  
FROM TableA AS a
JOIN TableB AS b ON a.Id=b.Id
WHERE YEAR(a.StartDate) >= 2018 AND b.RedemptionDate IS NULL
GROUP BY a.id
), cte2 AS (
SELECT a.id, COUNT(b.CodeId) AS 'Redeem'  
FROM TableA AS a
JOIN TableB AS b ON a.Id=b.Id
WHERE YEAR(a.StartDate) >= 2018 AND b.RedemptionDate IS NOT NULL
GROUP BY a.id
)
SELECT cte1.Id, cte1.Unlocked, cte2.Redeemed
FROM cte1
FULL OUTER JOIN cte2 ON cte1.Id = cte2.Id

如果我分解这个查询的输出,cte1的结果将如下所示:

| Id  | Unlock |
|-----|--------|
| 119 | 3      |
| 121 | 1      |
| 123 | 1      |

从cte2将看起来如下:

| Id  | Redeem |
|-----|--------|
| 119 | 1      |
| 120 | 2      |
| 121 | 2      |

最后一个选择查询将产生以下结果:

| Id   | Unlock | Redeem |
|------|--------|--------|
| 119  | 3      | 1      |
| null | null   | 2      |
| 121  | 1      | 2      |
| 123  | 1      | null   |

如何将Id中的null值替换为"b.Id"中的值?如果我尝试合并或case语句,它们会创建新的列。我不想创建额外的列,而是用来自另一个表的列值替换空值。我的最终输出应该是:

| Id  | Unlock | Redeem |
|-----|--------|--------|
| 119 | 3      | 1      |
| 120 | null   | 2      |
| 121 | 1      | 2      |
| 123 | 1      | null   |

如果我正确地遵循以下内容,您可以将apply与聚合一起使用:

select a.*, b.*
from a cross apply
(select count(RedemptionDate) as num_redeemed,
count(*) - count(RedemptionDate) as num_unlock
from b
where b.id = a.id
) b;

但是,您的问题的答案是使用coalesce(cte1.id, cte2.id) as id

相关内容

  • 没有找到相关文章

最新更新