主表应与给定列的查找表匹配,如果不匹配且查找表具有任何null值,则应与Null值匹配(此处null =任何值)
场景
CREATE TABLE dbo.MAIN_TABLE (
NAMEID VARCHAR(50) NOT NULL,
COL1 VARCHAR(50) NULL,
COL2 VARCHAR(50) NULL,
COL3 VARCHAR(50) NULL,
COL4 VARCHAR(50) NULL,
CONSTRAINT PK_MAIN_TABLE PRIMARY KEY (NAMEID)
)
CREATE TABLE dbo.LOOKUP_TABLE (
COL1 VARCHAR(50) NULL,
COL2 VARCHAR(50) NULL,
COL3 VARCHAR(50) NULL,
COL4 VARCHAR(50) NULL,
RES_COL VARCHAR(50) NOT NULL
)
TRUNCATE TABLE dbo.LOOKUP_TABLE
INSERT INTO dbo.LOOKUP_TABLE VALUES('AA','BB', 'CC', NULL, 'Rule1')
INSERT INTO dbo.LOOKUP_TABLE VALUES('AA','BB', NULL, NULL, 'Rule2')
INSERT INTO dbo.LOOKUP_TABLE VALUES('AA',NULL, NULL, NULL, 'Rule3')
INSERT INTO dbo.LOOKUP_TABLE VALUES(NULL,NULL, NULL, NULL, 'Rule4')
TRUNCATE TABLE dbo.MAIN_TABLE
INSERT INTO dbo.MAIN_TABLE VALUES('NameId1','AA','BB', 'CC', 'DD' )
INSERT INTO dbo.MAIN_TABLE VALUES('NameId2','AA','BB', 'CC', NULL )
INSERT INTO dbo.MAIN_TABLE VALUES('NameId3','AA','BB', NULL, NULL )
INSERT INTO dbo.MAIN_TABLE VALUES('NameId4','AA', NULL, NULL, NULL )
INSERT INTO dbo.MAIN_TABLE VALUES('NameId5',NULL,'BB', 'CC', 'DD' )
INSERT INTO dbo.MAIN_TABLE VALUES('NameId6',NULL,NULL, 'CC', 'DD' )
预期结果
NAMEID | RES_COL
-----------------
NameId1 | Rule1
NameId2 | Rule1
NameId3 | Rule2
NameId4 | Rule3
NameId5 | Rule4
NameId6 | Rule4
我尝试了以下查询,但返回的行与其他所有值重复
SELECT MN.NAMEID, MAP.RES_COL FROM MAIN_TABLE MN
CROSS APPLY
(
SELECT * FROM LOOKUP_TABLE LKP
WHERE
( CHECKSUM(MN.COL1) = CHECKSUM(LKP.COL1) OR LKP.COL1 IS NULL )
AND
( CHECKSUM(MN.COL2) = CHECKSUM(LKP.COL2) OR LKP.COL2 IS NULL)
AND
( CHECKSUM(MN.COL3) = CHECKSUM(LKP.COL3) OR LKP.COL3 IS NULL)
AND
( CHECKSUM(MN.COL4) = CHECKSUM(LKP.COL4) OR LKP.COL4 IS NULL )
) MAP
ORDER BY MN.NAMEID ASC
答案 0 :(得分:2)
由于NULL
=匹配所有规则,因此您获得了多个匹配项。因此,对于任何名称,Rule4
肯定会匹配,因为所有列均为NULL
如果您只想要最好的匹配项,请将TOP 1
与ORDER BY
一起使用
SELECT *
FROM dbo.MAIN_TABLE m
OUTER APPLY
(
SELECT TOP 1 *
FROM dbo.LOOKUP_TABLE l
WHERE (l.COL1 = m.COL1 OR l.COL1 IS NULL)
AND (l.COL2 = m.COL2 OR l.COL2 IS NULL)
AND (l.COL3 = m.COL3 OR l.COL3 IS NULL)
AND (l.COL4 = m.COL4 OR l.COL4 IS NULL)
ORDER BY
CASE WHEN l.COL1 IS NOT NULL THEN 1 ELSE 2 END
+ CASE WHEN l.COL2 IS NOT NULL THEN 1 ELSE 2 END
+ CASE WHEN l.COL3 IS NOT NULL THEN 1 ELSE 2 END
+ CASE WHEN l.COL4 IS NOT NULL THEN 1 ELSE 2 END
) l
您使用CHECKSUM()
而不是简单的等号=
的任何特殊原因吗?