我有一个包含姓名,相对姓名和年龄的表格如下
SNO 姓名 相对姓名 年龄
1 AAKASH SOOD RAJEEV SOOD 22
2 AAKASH SOOD RAJEEV SOOD 21
3 AAMITA Kumari RAJEEV VERMA 35
4 AAMITA Kumari RAJEEV VERMA 46
5 AAKASH SOOD RAJEEV SOOD 23
6 NARESH VERMA SHIV LAL SAHU 37
7 NARESH VERMA SHIV LAL SAHU 38
现在我想知道在SQL Server 2005数据库中是否可以提取重复记录,其名称,相对名称和年龄在+1或-1范围内。我的意思是只提取这些记录
SNO 姓名 相对姓名 年龄
1 AAKASH SOOD RAJEEV SOOD 22
2 AAKASH SOOD RAJEEV SOOD 21
5 AAKASH SOOD RAJEEV SOOD 23
6 NARESH VERMA SHIV LAL SAHU 37
7 NARESH VERMA SHIV LAL SAHU 38
感谢提前帮助和建议
答案 0 :(得分:1)
检查出来:
--create table sno (sno varchar(50), name varchar(100), rel_name varchar(100), age int)
--insert into sno values
--('1','AAKASH SOOD','RAJEEV SOOD','22'),
--('2','AAKASH SOOD','RAJEEV SOOD','21'),
--('3','AAMITA Kumari','RAJEEV VERMA','35'),
--('4','AAMITA Kumari','RAJEEV VERMA','46'),
--('5','AAKASH SOOD','RAJEEV SOOD','23'),
--('6','NARESH VERMA','SHIV LAL SAHU','37'),
--('7','NARESH VERMA','SHIV LAL SAHU','38')
select distinct n1.*
from sno n1
join sno n2 on n1.name = n2.name
and n1.rel_name = n2.rel_name
and n1.sno != n2.sno
and (n1.age = n2.age or n1.age = n2.age + 1 or n1.age = n2.age - 1)