以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  如何只保留最后一条记录  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=99545)

--  作者:明天的灵
--  发布时间:2017/4/21 10:17:00
--  如何只保留最后一条记录
有一表测试表,是各个设备最后的通信记录,现想同一个设备名只保留最后一条通讯记录,删除其余记录。
如CC设备名,有三条通信记录,只保留“CC   2017/3/3 15:28:00”这一条

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:保留最后一条.table


[此贴子已经被作者于2017/4/21 10:18:09编辑过]

--  作者:有点色
--  发布时间:2017/4/21 10:43:00
--  

参考代码

 

Dim idx As String = "-1,"
Dim idx_temp As String = ""
Dim pdr As DataRow = Nothing
Dim count As Integer = 0
Dim cs As String = "设备名"
For Each dr As DataRow In DataTables("测试").Select("", "设备名, 最后通信时间 desc")
    Dim flag As Boolean = False
    If pdr IsNot Nothing Then
        For Each c As String In cs.split(",")
            If pdr(c).replace(" ","") <> dr(c).replace(" ", "") Then
                flag = True
                Exit For
            End If
        Next
        If flag Then
            If count > 1 Then
                idx &= idx_temp
            End If
            count = 1
            idx_temp = ""
        Else
            count += 1
            idx_temp &= dr("_Identify") & ","
        End If
    Else
        count += 1
    End If
    pdr = dr
Next

If count > 1 Then
    idx &= idx_temp
End If

 

Tables("测试").Filter = "_Identify not In (" & idx.trim(",") & ")"

\'DataTables("测试").DeleteFor("_Identify not In (" & idx.trim(",") & ")")


 


--  作者:明天的灵
--  发布时间:2017/4/21 10:55:00
--  
谢谢版主