Sql 基于优先级字段合并记录

Sql 基于优先级字段合并记录,sql,sql-server,Sql,Sql Server,我有一个从多个来源汇编的人口统计信息表。因此,有些人有重复的记录(基于他们的标识符),在其他字段中有不同的数据。我添加了一个record_authority字段,它根据行的原始来源列出行的优先级。这就是它看起来的样子: record_authority identifier lastname firstname dob 1 1234 Doe William 1/1/2000 3

我有一个从多个来源汇编的人口统计信息表。因此,有些人有重复的记录(基于他们的标识符),在其他字段中有不同的数据。我添加了一个record_authority字段,它根据行的原始来源列出行的优先级。这就是它看起来的样子:

record_authority    identifier    lastname    firstname    dob
1                   1234          Doe         William      1/1/2000
3                   1234          Doe         Bill         1/1/2000
2                   5678          Smith       Jane         NULL
3                   5678          Smith       Jane         1/1/1990
1                   9999          Brown       John         NULL
4                   9999          Brown       John         NULL
这就是我想要的结局。空值应替换为最高优先级的非空值(如果可用)

identifier    lastname    firstname    dob
1234          Doe         William      1/1/2000
5678          Smith       Jane         1/1/1990
9999          Brown       John         NULL

非常感谢您的帮助

一种方法使用
行编号()

另一个使用相关子查询:

select t.*
from t
where t.record_authority = (select min(t2.record_authority)
                            from t t2
                            where t2.identifier = t.identifier
                           );

嗨,戈登。我应该提到,较低的record_authority值对应较高的优先级——我认为您的查询将其反转。此外,您的查询似乎正在使用整行,即使存在空值。是否有方法为给定字段输入第一个非空值(按优先级),如上面的结果表所示?@user56511。我建议你问一个更清楚的新问题。在这个问题中,返回的数据至少看起来可能来自一行。使用适当的样本数据和期望的结果。
select t.*
from t
where t.record_authority = (select min(t2.record_authority)
                            from t t2
                            where t2.identifier = t.identifier
                           );