交叉应用产生空值

Cross Apply yields nulls

我在引用 MS:

CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function.

这意味着它不会 return 具有空值的行,对吧? 但是,我的查询是:

select ....,cat_custom,....
from ...(various inner joins)...                
cross apply
                (
                    select  
                        case 
                            when i.cat1='01' then 1
                            when i.cat2='04' then 2
                            when i.cat2='07' then 3
                            when i.cat2 in ('08') or i.cat3 in ('014','847') then 4
                            else null 
                        end as cat_custom
                ) as cat_custom_query

...果然,我得到了空行。那不是 OUTER apply 的工作吗?这是怎么回事?

CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function.

在您的示例中,生成了一行 - 行,它返回一个 NULL 值。

你可以试试这个:

select ....,cat_custom,....
from ...(various inner joins)...                
cross apply
                (
                    select  
                        case 
                            when i.cat1='01' then 1
                            when i.cat2='04' then 2
                            when i.cat2='07' then 3
                            when i.cat2 in ('08') or i.cat3 in ('014','847') then 4
                            else null 
                        end as cat_custom
                    WHERE i.cat1 IN ('01', '04', '07', '08', '014', '847')
                ) as cat_custom_query

此外,如果这是您实际查询的一部分,您可以在 SELECT 语句中添加结果列。您不需要在此处使用 CROSS APPLY,因为您没有引用任何 SQL 对象(表、视图、函数等)。