在SQL中,主表应与多列的查找表匹配
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在SQL中,主表应与多列的查找表匹配相关的知识,希望对你有一定的参考价值。
主表应与给定列的查找表匹配,如果它不匹配且查找表具有任何空值,则它应与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
答案
你得到多个匹配因为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()
而不是简单的=
的任何特殊原因?
以上是关于在SQL中,主表应与多列的查找表匹配的主要内容,如果未能解决你的问题,请参考以下文章
sql中left join、right join、inner join有啥区别