ID Name Addr tDate
-------------------------------
| 1 | Aa | street | 20151231
| 2 | Aa | street | 20130202
| 2 | Aa | street | 20120101
| 3 | Aa | way | 20150821
| 4 | Bb | street | 20150821
| 7 | Xb | street | 20150821
| 5 | Cc | way | 20150821
| 5 | Cc | way | 20150821
| 6 | Cc | no way | 20150821
結果相同的值的唯一ID創建新的ID 01
ID Name Addr | tDate
-------------------------------
| 1 | Aa | street | 20151231
| 2 | Aa | street | 20130202
| 2 | Aa | street | 20120101
將創建一個新的nID
應當原樣複製或合併如果名稱和地址相同並且選擇了ID
和最新的tDate
結果02
ID Name Addr tDate nID
------------------------------------
| 1 | Aa | street | 20151231 | 1
| 2 | Aa | street | 20120101 | 1 <-- nID != ID
| 2 | Aa | street | 20130202 | 1 <-- nID != ID
| 3 | Aa | way | 20150821 | 3
| 4 | Bb | street | 20150821 | 4
| 7 | Xb | street | 20150821 | 7
| 5 | Cc | way | 20150821 | 5
| 5 | Cc | way | 20150821 | 5
| 6 | Cc | no way | 20150821 | 6
我試過這個。不知道它是否正確。
SELECT DISTINCT dr.*
FROM MyTable dr
inner join(
SELECT ID, Name, Addr
FROM MyTable
GROUP BY ID, Name, Addr
) ss on dr.Name = ss.Name and
dr.Addr = ss.Addr and
dr.ID <> ss.ID
order by Name
該死的抱歉.. BB和XB有不同的ID - 編輯 – Kaptah