繁体   English   中英

两列重复数据删除

[英]Deduplication by two columns

1我需要对这样的表进行重复数据删除:-

name | fix | mobile
-----+-----+---------
dan  | 1   | 1
jon  | 1   | 3
mia  | 1   | 4
ken  | 5   | 4

我想得到:

dan  | 1   | 1
jon  |     | 3
mia  |     | 4
ken  | 5   |  

可以使用Excel,但是对于50万行,我会'not responding' 。如果发现重复行,Access将删除整行。

例如:设置修复的主键,我得到:-

dan  | 1  | 2
ken  | 5  | 4

将主键设置为mobil ,我得到:

dan  | 1  | 1
jon  | 1  | 3
mia  | 1  | 4

SQL或其他程序可以这样做吗? 我尝试使用“不重复”或“分组依据”,但是找不到正确的公式。

在sql中是可能的。 我确实写了sql代码。假设您在表中有id列,并且您的表名是people 我编写了用于更新“ 修复 ”列的代码。 诀窍是将桌子与自己连接起来。 如果您有任何疑问,请随时提问。

    update temp1 set temp1.fix='' 
    from people temp1 inner join people  temp2 
    on temp1.fix=temp2.fix where temp1.id>temp2.id

使用此查询:

SELECT 
    t.NAME,
    IIF(t.partRankFix = 1, t.fix, NULL) AS fix,
    IIF(t.partRankMobile = 1, t.mobile, NULL) AS mobile
    ,A.field1, A.field2
FROM (
    SELECT 
        A.Name, A.Fix, A.mobile
        , Sum(IIF(A.fix = B.fix OR A.fix=B.mobile, 1, 0)) AS partRankFix
        , Sum(IIF(A.mobile = B.mobile AND A.mobile = B.fix, 2, 
              IIF(A.mobile = B.mobile OR A.mobile = B.fix, 1, 0))) AS partRankMobile
        , A.field1, A.field2
    FROM 
        yourTable AS A, 
        yourTable AS B
    WHERE 
        (((Nz(A.fix, 0) < Nz(B.fix, 0)) OR (Nz(B.fix, 0) = Nz(A.fix, 0))) AND (Nz(B.NAME, 0) >= Nz(A.NAME, 0)))
    GROUP BY 
        A.Name, A.Fix, A.mobile, A.field1, A.field2) AS t

这个模块应该完成工作。 我已经使用了名为edup的原始表(在这里我将其命名为table1 )的副本

Public Function Dedup()
Dim rstSource As DAO.Recordset
Dim rstDestination As DAO.Recordset
Dim deduppreviousValue As Long
Dim dedupValue As Long
Dim blnInit As Boolean

Set rstSource = CurrentDb.OpenRecordset("Select * from Table1 order by Fix,Mobile")
Set rstDestination = CurrentDb.OpenRecordset("dedup")

With rstSource
    .MoveFirst
    blnInit = False
    While Not .EOF
        dedupValue = .Fields("fix")

        rstDestination.AddNew
        rstDestination.Fields("NameID") = .Fields("nameID")
        If Not blnInit Then
            rstDestination.Fields("fix") = .Fields("fix")
            blnInit = True
        Else
            If deduppreviousValue <> dedupValue Then
                rstDestination.Fields("fix") = .Fields("fix")
            Else

            End If
        End If
        rstDestination.Fields("mobile") = .Fields("mobile")
        rstDestination.Update
        deduppreviousValue = .Fields("fix")
        .MoveNext
    Wend

End With
End Function

好的,我通过这个例子达到了目的。

-首先,我创建一个要使用的表:

CREATE TABLE #Table (field1 INT, field2 INT, field3 int)

-然后,我在字段中填写与您相似的数据:

 INSERT INTO #Table VALUES(1,2,3)

 INSERT INTO #Table VALUES(2,2,4)

 INSERT INTO #Table VALUES(3,2,5)

 INSERT INTO #Table VALUES(4,3,5)

-在下面的查询中,您可以最终找到所需的内容:

 SELECT t1.field1,t2.field2,t3.field3 from  #Table t1 
 left join 
 (select field2,min(field1) AS field1 from #Table group by field2) t2 
 on t1.field1 = t2.field1  left join 
 (select field3,min(field1) AS field1 from #Table group by field3) t3 
 on   t1.field1 = t3.field1

关键是要对要重复数据删除的每个字段进行左连接,并使其始终与相同的字段“ field1”匹配。 在每个表中,您都必须按要删除重复项的字段分组。

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM