交叉应用时如何返回行为空

问题描述 投票:1回答:1

我有一行数据想要添加列,所以我交叉应用了一些数据。

但是,所应用的表有时为空。

当应用的表为null时,不会返回任何内容。

表1

| a | b | c |
|---+---+---|
| 0 | 1 | 1 |

表2

| d | e | f |
|---+---+---|

查询

select top 1 
[col_1] = coalesce([a], [d], '') 
from table_1
cross apply ( select [d], [e] from table_2)

预期输出

| a | b | c |
|---+---+---|
| 0 | 1 | 1 |

实际输出

*null*

交叉应用Table 1时,无论数据是否可用,如何保留Table 2中的列?

注意:我正在尝试结合使用。

sql sql-server tsql coalesce cross-apply
1个回答
1
投票

使用outer apply

select top 1 
[col_1] = coalesce([a], [d], '') 
from table_1
outer apply ( select [d], [e] from table_2)
© www.soinside.com 2019 - 2024. All rights reserved.