【问题标题】:Alternative to Row_Number Over PartitionRow_Number Over Partition 的替代方案
【发布时间】:2020-03-31 18:51:45
【问题描述】:

我的数据格式如下:

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

我想要完成的是结合所有时间范围来使用生产类型。所以最终的结果是:

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 服务器,我可以通过使用 Row_Number Over Partition 来实现这一点。现在我只能访问 excel/access,我发现很难创建一种将数据“压缩”到仅 StateType 持续时间的方法。我拉取数据的系统无法更改,因此原始格式也无法修改。

【问题讨论】:

  • “人”是否代表姓名?如果是这样,而不是“Person”,应该在示例中使用一些假名,你真的需要在输出中使用名称吗?
  • 对不起,是的,列表中会有多人使用不同的名字。
  • 你有很多行数据吗?只是想知道是否值得考虑仅在 Excel 中使用解决方案,还是必须在 Access 中使用?
  • 好问题,出来的数据集相当大,有不少行。我可以做任何事情。我已经有一个宏可以稍微格式化原始文件(去掉一些垃圾标题行),所以我可以在那个时候或在导入访问之后添加它。

标签: excel vba ms-access reporting


【解决方案1】:

这可能不是解决此问题的最佳方法,但应该可以。您可能可以在 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 Sub

【讨论】:

  • 那么我需要为table2创建一个空白表吗?
  • 啊,是的,抱歉忘了提。
  • Table2 可能会被视为“临时”表 - 仅在处理期间保存数据,并且每次都会清除数据。所以可能想在程序开始时添加一行来删除所有记录。 CurrentDb.Execute "DELETE FROM Table2".
  • 在基于原表打开记录集时,可能应该使用带有“ORDER BY Agent, StartTime”的SQL语句,否则无法保证数据的顺序,这使得其余代码无法按预期工作。
  • 是的,虽然我不知道他们需要它做什么,所以我没有添加它。如果它是一个 excel 导出或其他东西,那么肯定。
猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 2018-10-16
  • 2013-02-24
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
相关资源
最近更新 更多