我的数据格式如下:
ID Agent AgentState StateType StartTime EndTime
Smith, Bob WaitingForCall Productive 08:00 10:00
Smith, Bob OnCall Productive 10:00 10:30
Smith, Bob LoggedOut NonProductive 10:30 10:45
Smith, Bob WaitingForCall Productive 10:45 11:00
Smith, Bob OnCall Productive 11:00 11:45
Smith, Bob OnCall Productive 11:45 12:15
Smith, Bob LoggedOut NonProductive 12:15 13:15我想要完成的是将所有时间范围组合在一起,只使用productive类型。因此,最终结果将是:
ID Agent StateType StartTime EndTime
Smith, Bob Productive 08:00 10:30
Smith, Bob NonProductive 10:30 10:45
Smith, Bob Productive 10:45 12:15
Smith, Bob NonProductive 12:15 13:15在之前的工作中,我可以访问SQL server,我可以通过使用分区上的Row_Number来完成这项工作。现在我只能访问excel/access,我发现很难创建一种方法来将数据“压缩”到只有StateType持续时间。我拉取数据的系统不能更改,所以原始格式也不能修改。
发布于 2020-04-01 03:28:21
这可能不是最好的方法,但它应该是可行的。你也许可以用SQL做这件事,但我不确定该怎么做。
这假设您的数据是有序的,如果不是,您可能需要定义一个基本容器类来将这些值保存到最后。
这将需要一个添加了相关列的新表。
Sub compress()
Dim db As Database
Dim rs As Recordset
Dim employee As String
Dim state As String
Dim starting As Date
Dim ending As Date
Set db = CurrentDb
Set rs = db.OpenRecordset("Table1") 'Use real table name
Dim appendtable As Recordset
Set appendtable = db.OpenRecordset("Table2") 'Use real table name
With rs
.MoveFirst
employee = ![agent]
state = ![statetype]
starting = Format(![starttime], "HH:MM") 'You probably don't need the format
ending = Format(![endtime], "HH:MM")
Do Until .EOF
If employee = ![agent] And state = ![statetype] Then
ending = Format(![endtime], "HH:MM") 'Track ending times
Else
appendtable.AddNew 'Append data
appendtable![agent] = employee
appendtable![statetype] = state
appendtable![starttime] = starting
appendtable![endtime] = ending
appendtable.Update
employee = ![agent] 'Reset Data
state = ![statetype]
starting = Format(![starttime], "HH:MM")
ending = Format(![endtime], "HH:MM")
End If
.MoveNext
Loop
End With
End Subhttps://stackoverflow.com/questions/60957843
复制相似问题