在 MS Access 中合并类似日期
我是一名飞行员,一天要飞行多段航程。我用来记录航班的软件会生成一个 csv 文件并分别列出每条航段。我将 csv 文件导入到 ms access 中的表 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(希望这不是问题)/ 日期字段是一个字符串,而不是日期/ 时间以分钟为单位,并且可以保持这种状态/ 表1/中的记录通常不会超过80条 一天内可能有一到八个航班/
I am a pilot who flies multiple legs in a day. The software I use to log flights spits out a csv file and lists every leg separately. I import the csv file into table 1 in ms access. I would like to merge all flights from the same day into one record on a new table. My problem is combining the route and adding the time.
Table 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
Table 2
Date Plane Route Time
2009-10-13 111WS CHO-LGA-ITH-LGA-BOS 420
I would like to use VBA code to do this, but I haven't done any programming in 12 years and unfortunately don't have the time to relearn. I don't think the code has to be too elaborate, it seems pretty straightforward. I just don't know how to do it. I hope someone can help me out. Thanks in advance.
Note:
I am using MS Access 97 (hope that's not a problem)/
The date field is a string, not a date/
The time is in minutes, and can stay that way/
There normally will not be more than 80 records in table 1/
There can be anywhere from one to eight flights in one day/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建总计查询,引入表格,并包含
日期
和时间
作为列。总计行中的日期列应设置为Group By
,时间应设置为Sum
。您还需要另一列来获取路线中的最终条目,因此请将To
列也放入网格中,并将该列的 Totals 行设置为Last
。要获取路线的其余部分,您需要使用如下组合函数:
返回子记录值的串联列表
http://www.mvps.org/access/modules/mdl0004.htm
这会将 FROM 列合并为单个值,您可以将其作为另一列包含在输出中。将此列的总计行设置为
表达式
。要获取完整路线,请将连接的 FROM 列与 LAST TO 列组合起来。
请注意,您不需要立即构建整个查询。单独构建这三个部分(总时间、串联路线、结束目的地)(在其自己的查询中),并确保每个部分单独工作,然后将它们组合成单个查询。
Create a Totals query, bring in your table, and include the
Date
andTime
as columns. The Date Column should be set toGroup By
in the Total Row, and the Time should be set toSum
. You will also need another column to get the final entry in the route, so put theTo
column in the grid also, and set the Totals row for that column toLast
.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.
添加模块
并运行查询
在我的上工作过
Add module
and run query
Worked on mine
与 ACE (Access 2007) 不同,Jet 3.51 引擎 (Access97) 没有多值类型。 SQL 语言(包括 Access 数据库引擎自己的专有 SQL)没有“连接”函数,因为这会违反需要标量类型的第一范式 (1NF)。所以这不适合 SQL 查询。在我看来,这更像是一份报告的候选人。
说到 1NF,考虑到一天内可能两次飞往同一目的地,您的表缺少关系键。听起来您需要将输入为“文本”的单个“日期”列替换为一对表示周期的
DATETIME
值,以及所需的“顺序主键”,例如CHECK
约束以防止周期重叠。时态数据库绝对不简单!Unlike ACE (Access 2007), the Jet 3.51 engine (Access97) doesn't have multivalue types. SQL the language (including the Access Database Engine's own proprietary SQL) does not have a 'Concatenate' function because it would be a violation of first normal form (1NF) which requires scalar types. So this isn't something for a SQL query. Sounds to me more like a candidate for a report.
Speaking of 1NF, considering it is possible to fly to the same destination twice in one day, your table lacks a relational key. It sounds like you need to replace you single 'date' column that is typed as 'text' with a pair of
DATETIME
values representing a period, with the required 'sequenced primary key' e.g. aCHECK
constraint to prevent overlapping periods. Temporal databases are definitely non-trivial!感谢您的所有回复。我使用了“THEn”的答案,但我必须改变一些东西(希望这不是问题)。我只需要按日期分组的航班,因此我取出按飞机分组,并只记录当天第一段航班的第一架飞机。另外,我刚刚发现我的软件以相反的顺序导出 csv 文件,因此我稍微更改了模块以解决此问题。这就是导入的数据的样子(我以 CHO 开始和结束):
这是模块:
这是查询:
这会导致问题,因为我在 openrecordset 行中使用名为“From”的字段,我尝试过将字段重命名为其他名称,效果非常好。不过,我希望保留字段名称的原样。当我在 openrecordset 行中使用字段名称“To”时,它起作用了,但后来我遇到了数据顺序相反的问题。所以我正在寻找任何建议,但我想保持字段名称相同,并且如果可能的话,我想保持表的顺序相反。再次感谢你们。
Thanks for all your responses. I used "THEn's" answer, but I had to change a few things (hope that's not a problem). I only needed the flights grouped by date, so I took out the grouping by plane, and just logged the first plane on the first leg of that day. Also I just found out that my software exports the csv file in reverse order, so I changed the module a little to account for this. So this is what the imported data looks like (I start and end in CHO):
This is the Module:
This is the query:
This causes a problem because I'm using a field called "From" in the openrecordset line, I tried renaming the field to something else and it worked perfectly. However I was hoping to keep the field names the way they are. It worked when I was using the field name "To" in the openrecordset line, but then I was running into a problem with the data being in reverse order. So I was looking for any suggestions, but I would like to keep the field names the same, and I would like to keep the table in reverse order if possible. Thanks again guys.