【发布时间】:2018-12-11 15:51:41
【问题描述】:
我正在尝试用姓名首字母、姓氏和出生日期匹配表中的人
我关注了this answer,这有助于减少与比当前 ID 更早的 ID 匹配的数量(因此 3 匹配 1,但 1 不匹配 3)
我遇到了 1,2 和 3 匹配的问题,结果有
3-2
3-1
2-1
我怎样才能让 3 只匹配 1?
这是我正在使用的代码
select t1.id, t1.date, t1.initial, t1.lastname, t1.birth, min (t2.id)
from
YourTable t1
join YourTable t2 on t1.date< t2.date --date when the record was created
and t1.initial= t2.initial
and t1.lastname= t2.lastname
and t1.birth= t2.birth
Group by t1.id, t1.date, t1.initial, t1.lastname, t1.birth
我在之前的问题my previous quesiton 中也得到了回复,但经过反复试验,结果发现使用的 ID 不是数字而是哈希,所以 MIN() 并不总是有效。
因此,查询的有效输出将是select t1.id, t1.date, t1.initial, t1.lastname, t1.birth, min (t2.date)
但后来我不知道 t2.id
获取 t2.id 的有效方法是什么?
样本数据:
+-------+------------+-------+------------+-----------+------------+-------------+-----------+
| T1.ID | T1.Date | T2.ID | T2.Date | T1.Birth | T1.initial | T1.lastname | T1.gender |
+-------+------------+-------+------------+-----------+------------+-------------+-----------+
| 0x547 | 4/7/2018 | 0xB29 | 11/2/2013 | 8/10/1956 | A | B | 1 |
| 0x547 | 4/7/2018 | 0x75D | 11/26/2013 | 8/10/1956 | A | B | 1 |
| 0x75D | 11/26/2013 | 0xB29 | 11/2/2013 | 8/10/1956 | A | B | 1 |
| 0xD50 | 5/18/2018 | 0xB29 | 11/2/2013 | 8/10/1956 | A | B | 1 |
| 0xD50 | 5/18/2018 | 0x75D | 11/26/2013 | 8/10/1956 | A | B | 1 |
| 0xD50 | 5/18/2018 | 0x547 | 4/7/2018 | 8/10/1956 | A | B | 1 |
| 0x817 | 5/20/2018 | 0xB29 | 11/2/2013 | 8/10/1956 | A | B | 1 |
| 0x817 | 5/20/2018 | 0x75D | 11/26/2013 | 8/10/1956 | A | B | 1 |
| 0x817 | 5/20/2018 | 0x547 | 4/7/2018 | 8/10/1956 | A | B | 1 |
| 0x817 | 5/20/2018 | 0xD50 | 5/18/2018 | 8/10/1956 | A | B | 1 |
| 0x013 | 9/28/2018 | 0xB29 | 11/2/2013 | 8/10/1956 | A | B | 1 |
| 0x013 | 9/28/2018 | 0x75D | 11/26/2013 | 8/10/1956 | A | B | 1 |
| 0x013 | 9/28/2018 | 0x547 | 4/7/2018 | 8/10/1956 | A | B | 1 |
| 0x013 | 9/28/2018 | 0x817 | 5/20/2018 | 8/10/1956 | A | B | 1 |
| 0x013 | 9/28/2018 | 0xD50 | 5/18/2018 | 8/10/1956 | A | B | 1 |
+-------+------------+-------+------------+-----------+------------+-------------+-----------+
【问题讨论】:
-
请提供此问题的示例数据和预期结果。参考其他问题可能有助于补充信息,但每个问题都应该有自己的完整信息。
-
感谢 EzLo 的编辑修复!
标签: sql sql-server