关键是使用带有 OR 的 case 语句。请参阅下面的脚本...它可以在测试数据库中重新运行。
drop table family
drop table [address]
drop table adress2
go
CREATE Table family (
id_family int
, familyName varchar(50) NOT NULL
)
CREATE TABLE [address] (
id_family int
, street_name varchar(50)
)
CREATE TABLE adress2 (
id_family int
, cve_col varchar(50)
)
GO
INSERT family(id_family, familyName)
SELECT 1, 'Jones'
UNION SELECT 2, 'Smith'
UNION SELECT 3, 'Brown'
INSERT [address] (id_family, street_name)
SELECT 1, 'Jones St.'
UNION SELECT 2, ''
UNION SELECT 3, NULL
INSERT adress2(id_family, cve_col)
SELECT 1, ''
UNION SELECT 2, 'smith pl.'
UNION SELECT 3, 'brown ln.'
SELECT a.familyName
, street_name = CASE WHEN b.street_name is NULL OR b.street_name = '' THEN C.cve_col
ELSE b.street_name
END
FROM family a
INNER JOIN [address] b ON A.id_family = b.id_family
INNER JOIN [adress2] C ON A.id_family = C.id_family