[英]How can I insert rows based previous and next record data into MS Access time sequenced data?
In MS Access, My Table is like this:在 MS Access 中,我的表是这样的:
ProductName , Date , Time , Price
Apple , 05-April-2020, 9:15:59 , 110
Apple , 05-April-2020, 9:16:59 , 112
Apple , 05-April-2020, 9:17:59 , 108
Apple , 05-April-2020, 9:18:59 , 125
Apple , 05-April-2020, 9:20:59 , 110
Apple , 06-April-2020, 10:20:59 , 85
Apple , 06-April-2020, 10:21:59 , 82
Apple , 06-April-2020, 10:22:59 , 86
Apple , 06-April-2020, 10:25:59 , 84
Orange , 05-April-2020, 2:15:59 PM , 110
Orange , 05-April-2020, 2:16:59 PM , 112
Orange , 05-April-2020, 2:17:59 PM , 108
Orange , 05-April-2020, 2:18:59 PM , 125
Orange , 05-April-2020, 2:20:59 PM , 110
Orange , 10-April-2020, 2:21:59 , 85
Orange , 10-April-2020, 2:22:59 , 82
Orange , 10-April-2020, 2:26:59 , 86
Orange , 10-April-2020, 2:27:59 , 84
The data is from 9:15:59 AM to 3:29:59 PM for each date (always 59th second) for each product (thousands) for 4 years data.数据是从上午 9:15:59 到下午 3:29:59 的每个日期(总是第 59 秒),每个产品(千)的 4 年数据。 I need to fill up the gaps for each product for every date, eg我需要在每个日期填补每种产品的空白,例如
9:19 AM for apple on 05 April-2020
10:23 AM for apple on 05 April-2020
10:24 AM for apple on 05 April-2020
so on.很快。 Insert new records wherever needed copying the previous record price, coresponding productName and date, and new time.在需要的地方插入新记录,复制以前的记录价格、对应的产品名称和日期以及新时间。 If it can be (9:15 + 9:17 price)/2, for 9:16 even better.如果可以是(9:15 + 9:17 价格)/2,9:16 就更好了。 Only wherever its missing.只有在它失踪的地方。 Each Day for a specific product, if it has existence on that day, should have 375 rows coresponding to the 375 minutes from 9:15 to 3:30.特定产品的每一天,如果它在当天存在,则应该有 375 行对应于从 9:15 到 3:30 的 375 分钟。 Ideally there should be no more than 5-10 inserts required per product per day.理想情况下,每个产品每天需要的插件不超过 5-10 个。 If we can generate a report, we can track how many inserts were made to track any false entries.如果我们可以生成报告,我们可以跟踪进行了多少插入以跟踪任何错误条目。
Thanks a lot, Looking forward:)非常感谢,期待:)
I've put together some code that generates the missing records, including carrying over the last value from the previous day if needed.我已经整理了一些生成缺失记录的代码,包括如果需要的话,保留前一天的最后一个值。
However, I'm making absolutely no guarantees about the speed!!但是,我绝对不能保证速度!
Sub sMissingPrice()
On Error GoTo E_Handle
Dim db As DAO.Database
Dim rsDay As DAO.Recordset
Dim rsLookup As DAO.Recordset
Dim dtmTemp As Date
Dim strSQL As String
Const JetDateFmt = "\#mm\/dd\/yyyy\#;;;\N\u\l\l"
Const JetTimeFmt = "\#hh\:nn\:ss\#;;;\N\u\l\l"
Set db = DBEngine(0)(0)
Set rsDay = db.OpenRecordset("SELECT DISTINCT ProductName, ProductDate FROM tblProductPrice ORDER BY ProductName, ProductDate;")
If Not (rsDay.BOF And rsDay.EOF) Then
Do
dtmTemp = #9:15:59 AM#
' make sure that there is a value for 09:15:59. otherwise get the last price from the previous day
strSQL = "SELECT ProductTime, Price FROM tblProductPrice " _
& " WHERE ProductName='" & rsDay!ProductName & "' AND ProductDate=" & Format(rsDay!ProductDate, JetDateFmt) & " AND ProductTime=" & Format(dtmTemp, JetTimeFmt)
Set rsLookup = db.OpenRecordset(strSQL)
If (rsLookup.BOF And rsLookup.EOF) Then
Set rsLookup = db.OpenRecordset("SELECT Price FROM tblProductPrice " _
& " WHERE ProductName='" & rsDay!ProductName & "' AND ProductDate=" & Format(rsDay!ProductDate - 1, JetDateFmt) & " AND ProductTime=" & Format(#3:29:59 PM#, JetTimeFmt))
If Not (rsLookup.BOF And rsLookup.EOF) Then
db.Execute "INSERT INTO tblProductPrice (ProductName,ProductDate,ProductTime,Price) " _
& " SELECT '" & rsDay!ProductName & "'," & Format(rsDay!ProductDate, JetDateFmt) & "," & Format(dtmTemp, JetTimeFmt) & "," & rsLookup!Price
End If
End If
' now loop through each minute of the day checking to see if we have data
Do
strSQL = "SELECT Price FROM tblProductPrice " _
& " WHERE ProductName='" & rsDay!ProductName & "' AND ProductDate=" & Format(rsDay!ProductDate, JetDateFmt) & " AND ProductTime=" & Format(dtmTemp, JetTimeFmt)
Set rsLookup = db.OpenRecordset(strSQL)
If (rsLookup.BOF And rsLookup.EOF) Then
Set rsLookup = db.OpenRecordset("SELECT Price FROM tblProductPrice " _
& " WHERE ProductName='" & rsDay!ProductName & "' AND ProductDate=" & Format(rsDay!ProductDate, JetDateFmt) & " AND ProductTime=" & Format(DateAdd("n", -1, dtmTemp), JetTimeFmt))
If Not (rsLookup.BOF And rsLookup.EOF) Then
db.Execute "INSERT INTO tblProductPrice (ProductName, ProductDate, ProductTime, Price) " _
& " SELECT '" & rsDay!ProductName & "'," & Format(rsDay!ProductDate, JetDateFmt) & "," & Format(dtmTemp, JetTimeFmt) & "," & rsLookup!Price
End If
End If
dtmTemp = DateAdd("n", 1, dtmTemp)
Loop Until dtmTemp > #3:30:00 PM#
rsDay.MoveNext
Loop Until rsDay.EOF
End If
sExit:
On Error Resume Next
rsDay.Close
rsLookup.Close
Set rsDay = Nothing
Set rsLookup = Nothing
Set db = Nothing
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "sMissingPrice", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards,问候,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.