3

我是一名飞行员,一天能飞多条腿。我用来记录航班的软件会输出一个 csv 文件并分别列出每条航线。我在 ms 访问中将 csv 文件导入表 1。我想将同一天的所有航班合并到一张新表上的一条记录中。我的问题是结合路线并增加时间。

表格1

   Date       Plane     From     To     Time
2009-10-13    111WS     CHO      LGA    120
2009-10-13    111WS     LGA      ITH    100
2009-10-13    111WS     ITH      LGA     90
2009-10-13    111WS     LGA      BOS    110

表 2

   Date       Plane          Route            Time
2009-10-13    111WS    CHO-LGA-ITH-LGA-BOS     420

我想使用 VBA 代码来做到这一点,但我已经 12 年没有做过任何编程,很遗憾没有时间重新学习。我认为代码不必太复杂,看起来很简单。我只是不知道该怎么做。我希望有人可以帮助我。提前致谢。

注意:我使用的是 MS Access 97(希望这不是问题)/日期字段是字符串,而不是日期/时间以分钟为单位,可以保持不变/表中通常不会超过 80 条记录1/一天可以有1到8个航班/

4

4 回答 4

3

Create a Totals query, bring in your table, and include the Date and Time as columns. The Date Column should be set to Group By in the Total Row, and the Time should be set to Sum. You will also need another column to get the final entry in the route, so put the To column in the grid also, and set the Totals row for that column to Last.

To get the remainder of the route, you will need to use a combining function like this one:

Return a concatenated list of sub-record values
http://www.mvps.org/access/modules/mdl0004.htm

This will combine the FROM column into a single value, which you can include as another column in the output. Set the Total row for this column to Expression.

To get the complete route, combine the concatenated FROM columm with the LAST TO column.

Note that you don't need to build the entire query at once. Build each of the three pieces (total time, concatenated route, ending destination) individually (in its own query), and make sure each piece works individually, before combining them into a single query.

于 2009-10-19T21:01:47.343 回答
0

感谢您的所有回复。我使用了“THEn”的答案,但我不得不改变一些事情(希望这不是问题)。我只需要按日期分组的航班,所以我按飞机取出分组,并记录了当天第一段的第一架飞机。此外,我刚刚发现我的软件以相反的顺序导出 csv 文件,因此我稍微更改了模块以解决此问题。所以这就是导入数据的样子(我以 CHO 开始和结束):

  Date       Plane     From     To     Time
2009-10-14    111WS     LGA      CHO    120
2009-10-14    111WS     BOS      LGA    110
2009-10-13    111WS     LGA      BOS    110
2009-10-13    111WS     ITH      LGA     90
2009-10-13    111WS     LGA      ITH    100
2009-10-13    111WS     CHO      LGA    120

这是模块:

Public Function ConcatField(FieldName As String, TableName As String, Where As String, Optional Delimeter = "-") As String

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT " & FieldName & " FROM " & TableName & " WHERE " & Where)
rs.MoveLast
While Not rs.BOF
    ConcatField = ConcatField + IIf(ConcatField = "", "", Delimeter) + rs.Fields(0)
    rs.MovePrevious
Wend
ConcatField = ConcatField + "-" + DLookup("To", "rte", Where)
rs.Close
Set rs = Nothing
End Function

这是查询:

SELECT rte.Date, First(rte,plane), ConcatField("From","rte","Date='" & [Date] & "'") AS Expr1, Sum(rte.time) AS [Total Time]
FROM rte
GROUP BY rte.Date;

这会导致问题,因为我在 openrecordset 行中使用了一个名为“From”的字段,我尝试将该字段重命名为其他内容,并且效果很好。但是我希望保持字段名称的原样。当我在 openrecordset 行中使用字段名称“To”时它起作用了,但后来我遇到了数据顺序相反的问题。所以我一直在寻找任何建议,但我想保持字段名称相同,并且如果可能的话,我想保持表格的顺序相反。再次感谢各位。

于 2009-10-21T21:01:45.913 回答
0

添加模块

Public Function ConcatField(FieldName As String, TableName As String, Where As String, Optional Delimeter = "-", Optional OrderBy = "") As String
    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT " & FieldName & " FROM " & TableName & " WHERE " & Where & IIf(OrderBy > "", " ORDER BY " & OrderBy, ""))
    ConcatField = DLookup("From", "RTE", Where)
    While Not rs.EOF
        ConcatField = ConcatField + IIf(ConcatField = "", "", Delimeter) + rs.Fields(0)
        rs.MoveNext
    Wend
    rs.Close
    Set rs = Nothing
End Function

并运行查询

在我的工作

SELECT rte.Date, rte.Plane, ConcatField("to","rte","Date='" & [Date] & "' AND Plane='" & [Plane] & "'") AS Expr1, Sum(rte.Time) AS SumOfTime
FROM rte
GROUP BY rte.Date, rte.Plane, ConcatField("to","rte","Date='" & [Date] & "' AND Plane='" & [Plane] & "'");
enter code here
于 2009-10-19T22:50:54.777 回答
0

与 ACE (Access 2007) 不同,Jet 3.51 引擎 (Access97) 没有多值类型。SQL 这种语言(包括 Access 数据库引擎自己的专有 SQL)没有“连接”功能,因为它违反了需要标量类型的第一范式 (1NF)。所以这不适用于 SQL 查询。在我看来,这更像是报告的候选人。

说到 1NF,考虑到一天内有可能两次飞往同一个目的地,您的表缺少关系键。听起来您需要将键入为“文本”的单个“日期”列替换为一对DATETIME表示句点的值,并使用所需的“顺序主键”,例如CHECK防止重叠句点的约束。时态数据库绝对不是微不足道的!

于 2009-10-20T11:07:52.033 回答