複雜的查詢最好通過將其分解成碎片並逐步工作來解決。
首先,讓我們創建一個查詢來找到我們想要保留該行的重點,通過查找最近的每封電子郵件創建日期然後加入拿到編號:
select x.Email, x.CreateDate, x.Id
from myTable x
join (
select Email, max(CreateDate) as CreateDate
from myTable
group by Email
) y on x.Email = y.Email and x.CreateDate = y.CreateDate
好了,現在讓我們做查詢得到重複的電子郵件地址:
select Email
from myTable
group by Email
having count(*) > 1
,加入這個查詢回表以獲取有重複的每一行按鍵:
select x.Email, x.Id, x.CreateDate
from myTable x
join (
select Email
from myTable
group by Email
having count(*) > 1
) y on x.Email = y.Email
太好了。現在,所有剩下的就是加入第一個查詢與這一個讓我們的結果:
select keep.Email, keep.Id as IdKeep, keep.CreateDate as CreateDateOfIdKeep,
dup.Id as DuplicateId, dup.CreateDate as CreateDateOfDuplicateId
from (
select x.Email, x.CreateDate, x.Id
from myTable x
join (
select Email, max(CreateDate) as CreateDate
from myTable
group by Email
) y on x.Email = y.Email and x.CreateDate = y.CreateDate
) keep
join (
select x.Email, x.Id, x.CreateDate
from myTable x
join (
select Email
from myTable
group by Email
having count(*) > 1
) y on x.Email = y.Email
) dup on keep.Email = dup.Email and keep.Id <> dup.Id
注意最後keep.Id <> dup.Id
謂詞的加入保證了我們沒有得到在同一行兩個keep
和dup
。
剃刀SQL是一個接口,而不是一個數據庫。你用什麼數據庫? – 2015-04-02 01:50:36