有人可以告诉我为什么COALESCE在这里执行第一个SELECT而不是其他两个吗?我仍然在后两个语句中得到NULL值。
(SELECT COALESCE(DEFax, NULL, '') FROM Debtor d WHERE d.DEIsPrimary = 1 AND d.CApKey = c.CApKey) AS FaxNumberOne,
(SELECT COALESCE(DEFax, NULL, '') FROM (SELECT ROW_NUMBER() OVER (ORDER BY DEpKey ASC)
AS rownumber, DEFax FROM Debtor d WHERE d.CApKey = c.CApKey AND d.DEIsPrimary <> 1)
AS foo WHERE rownumber = 1) AS FaxNumberTwo,
(SELECT COALESCE(DEFax, NULL, '') FROM (SELECT ROW_NUMBER() OVER (ORDER BY DEpKey ASC)
AS rownumber, DEFax FROM Debtor d WHERE d.CApKey = c.CApKey AND d.DEIsPrimary <> 1)
AS foo WHERE rownumber = 2) AS FaxNumberThree
谢谢!
答案 0 :(得分:1)
样本数据和预期结果确实会有所帮助。
但是标量子查询是返回一列和零或一行的子查询。如果返回零行,则无论NULL
中的表达式如何,该值为SELECT
。换句话说,COALESCE()
需要走到外面,像这样:
coalesce( (select . . . . ),
''
)
在NULL
列表中包含coalesce()
不是一个好习惯。这是不必要的且具有误导性-始终被忽略。