0

我需要提取名字、姓氏和出生日期相同的记录。请找到以下示例。

Employeeid   firstname lastname DOB
00010         ravi      sagi     22/01/1990
00035         ravi      sagi     22/01/1990
00060         vasanth   guptha   20/01/1987
00115         vasanth   guptha   20/01/1987

你能帮忙写查询吗?

4

2 回答 2

4

尝试这个:

select *
from
  (
    select *,
           count(*) over(partition by firstname, lastname, DOB) as CC
    from YourTable
  ) as T
where T.CC > 1
于 2012-08-27T13:44:53.503 回答
1

您可以JOIN将表与自身进行比较firstnamelastnameDOB确保它们是相同的值,然后employeeid是不一样的:

select * 
from yourtable t1
inner join yourtable t2
  on t1.firstname = t2.firstname
  and t1.lastname = t2.lastname
  and t1.dob = t2.dob
  and t1.empid != t2.empid

上面的查询可能会显示重复的记录,因此您可以使用以下内容(请参阅SQL Fiddle with Demo):

select DISTINCT t1.empid,
  t1.firstname,
  t1.lastname,
  t1.DOB
from yourtable t1
inner join yourtable t2
  on t1.firstname = t2.firstname
  and t1.lastname = t2.lastname
  and t1.dob = t2.dob
  and t1.empid != t2.empid

或者您可以使用EXISTS(参见SQL Fiddle with Demo):

select t1.empid,
  t1.firstname,
  t1.lastname,
  t1.DOB
from yourtable t1
where exists (SELECT *
              FROM yourtable t2
              WHERE t1.firstname = t2.firstname
                and t1.lastname = t2.lastname
                and t1.dob = t2.dob
                and t1.empid != t2.empid)
于 2012-08-27T13:47:33.933 回答