我需要提取出名,姓和出生日期相同的記錄。 請看下面的例子。如何從員工表中提取出'姓,名和出生日期'同樣的員工表
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
你可以請幫忙寫下查詢。
我需要提取出名,姓和出生日期相同的記錄。 請看下面的例子。如何從員工表中提取出'姓,名和出生日期'同樣的員工表
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
你可以請幫忙寫下查詢。
試試這個:
select *
from
(
select *,
count(*) over(partition by firstname, lastname, DOB) as CC
from YourTable
) as T
where T.CC > 1
可以JOIN
將表與自身比較firstname
,lastname
和DOB
以確保它們是相同的值,然後將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)
嗨Bluefeet,非常感謝你:)得到它。 – user1627800
萬一有超過2人具有相同的姓名和相同的出生,這個查詢將返回(行1)*行,所以7個相同的生日將返回42行 –
@ t-clausen.dk你是正確的,我會可能在查詢中包含一個「DISTINCT」以刪除任何dups ...請參閱我的編輯。 – Taryn
感謝您的Mikael :) – user1627800