我有一个场景,我需要根据一个人的多个属性“匹配”记录。例如,如果FirstName和LastName匹配,或者NickName和LastName匹配,则应将这两个场景分组为一个较大的匹配。这是SQLFiddle中的示例数据: http://www.sqlfiddle.com/#!18/0ca91/7
我正在从记录属性生成匹配键。结果给了我两个不同的匹配键和三个总记录。我需要一个只生成一个匹配键的结果,最后我将在一个单独的步骤中将所有三个记录分组为一个黄金记录。我无法通过“分组依据”或使用DENSE_RANK来生成我的匹配键,从逻辑上将这些记录组合在一起。任何帮助将不胜感激!谢谢!
CREATE TABLE Persons (
ID int,
FirstName varchar(255),
LastName varchar(255),
NickName varchar(255)
);
INSERT INTO Persons
SELECT 1 AS ID, 'NIKKI' AS FNAME, 'MADISON' AS LNAME, 'Nikki' AS NickName
UNION ALL
SELECT 2 AS ID, 'NICOLE' AS FNAME, 'MADISON' AS LNAME, 'NICOLE' AS NickName
UNION ALL
SELECT 3 AS ID, 'NICOLE' AS FNAME, 'MADISON' AS LNAME,'Nikki' AS NickName
SELECT
*
, DENSE_RANK() OVER (ORDER BY TRIM(LastName), TRIM(FirstName)) AS GroupKey
FROM Persons
期望的结果:
GroupKey
1
1
1
答案 0 :(得分:0)
select distinct p1.*,
DENSE_RANK() OVER (ORDER BY TRIM(p1.LastName)) AS GroupKey
from persons p1
join persons p2 on (p1.FirstName = p2.FirstName or p1.FirstName = p2.nickname)