访问 SQL:根据条件更新一对多关系中的 Children
Access SQL: Updating Children in a 1 to Many relationship based on criteria
我的问题是一个一对多关系,其中 children 存储在 1 个 ChildTable,
ChildID | ParentID | Data2 | InputDate
------------------------------------
1 | 345 | 100 | 3-5-2016
2 | 345 | 0 | 3-12-2016
3 | 345 | 150 | 3-19-2016
4 | 345 | 0 | 4-20-2016
... more children with different parent IDs
并且考虑到 parents 存储在它们自己的带有 ParentID 的 ParentTable 中...数据等
我的问题是分区或更新如何查询类似的数据库,以便如果数据列的值为 0,则它会更新为最后的输入数据。 (假设网络抓取工具无法提取数据,我想进行近似分析)。原始数据不必在基础 table 上更新,但也可以在拉取它的查询中更新。我相信有一种方法可以使用 SQL 来实现。我的尝试非常慢,而且由于未知原因没有成功..
我尝试编写一些 VBA-Access 代码来遍历平面文件 ChildTable + ParentTable 并使用 SQL 查找更新数据列中的每个 0 值单元格。如果单元格 = 0,则转到非零的最后可用数据。
问题是这需要数年时间才能 运行.
Option Compare Database
Sub SoldOut()
On Error GoTo ProcError
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("MainDataTable") 'This is the flat file version of the data above
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveLast
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
GetLastWeek = 0
If Not rs("Data") > 0 Then
rs.Edit
rs("Data") = GetLastWeek(rs('ChildID'), rs('ParentID'), rs('Data'), rs('InputDate'))
rs.Update
End If
'Move to the next record. Don't ever forget to do this.
rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
ProcExit:
On Error Resume Next
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
Exit Sub
ProcError:
MsgBox Err.Description
Resume ProcExit
End Sub
Private Function GetLastWeek(ChildID, ParentID As Long, InputDate As Date) As Integer
'given a record it looks up the weeks before and returns it if it exists
Dim rst As DAO.Recordset, strSQL As String, rc As Integer ' SQL Code for seeing if last week's data exists.
strSQL = "SELECT * " & _
"FROM MainDataTable " & _
"WHERE MainDataTable.[ParentId] = " & ParentID & "AND MainDataTable.[InputDate] <# " & InputDate & "AND NOT MainDataTable.[Data] = 0
ORDER BY MainDataTable.[InputDate] DESC;"
Set rst = CurrentDb.OpenRecordset(strSQL): rst.MoveLast: rst.MoveFirst
rc = rst.RecordCount
If rc = 0 Then GoTo Cleanup 'if no record, then we are out of luck
If rc > 0 Then 'If there's some Record
Do Until rs.EOF = True Or GetLastWeek > 0
Dim price As Integer: price = rst("Data")
If price > 0 Then: GetLastWeek = price
rs.MoveNext
Loop
End If
Cleanup:
rst.Close
Set rst = Nothing
If GetLastWeek = 0 Then GetLastWeek = 1 '1 means no data was found
'Set so the output if nothing is found to 1 so that the code doesn't have to run on the same rows every single week
End Function
我不确定你所拥有的是如何工作的,但你所要做的就是遍历 parentid, date asc
订购的主 table,然后保存 parentID 和数据值只要它不为零,就会在下一次迭代 (Dim lDataValPrev as long
) 中使用。如果当前数据值为零且 parentID 未更改,请将其替换为您存储的先前值。您只需检查一次数据,无需额外调用。
我的问题是一个一对多关系,其中 children 存储在 1 个 ChildTable,
ChildID | ParentID | Data2 | InputDate
------------------------------------
1 | 345 | 100 | 3-5-2016
2 | 345 | 0 | 3-12-2016
3 | 345 | 150 | 3-19-2016
4 | 345 | 0 | 4-20-2016
... more children with different parent IDs
并且考虑到 parents 存储在它们自己的带有 ParentID 的 ParentTable 中...数据等
我的问题是分区或更新如何查询类似的数据库,以便如果数据列的值为 0,则它会更新为最后的输入数据。 (假设网络抓取工具无法提取数据,我想进行近似分析)。原始数据不必在基础 table 上更新,但也可以在拉取它的查询中更新。我相信有一种方法可以使用 SQL 来实现。我的尝试非常慢,而且由于未知原因没有成功..
我尝试编写一些 VBA-Access 代码来遍历平面文件 ChildTable + ParentTable 并使用 SQL 查找更新数据列中的每个 0 值单元格。如果单元格 = 0,则转到非零的最后可用数据。 问题是这需要数年时间才能 运行.
Option Compare Database
Sub SoldOut()
On Error GoTo ProcError
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("MainDataTable") 'This is the flat file version of the data above
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveLast
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
GetLastWeek = 0
If Not rs("Data") > 0 Then
rs.Edit
rs("Data") = GetLastWeek(rs('ChildID'), rs('ParentID'), rs('Data'), rs('InputDate'))
rs.Update
End If
'Move to the next record. Don't ever forget to do this.
rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
ProcExit:
On Error Resume Next
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
Exit Sub
ProcError:
MsgBox Err.Description
Resume ProcExit
End Sub
Private Function GetLastWeek(ChildID, ParentID As Long, InputDate As Date) As Integer
'given a record it looks up the weeks before and returns it if it exists
Dim rst As DAO.Recordset, strSQL As String, rc As Integer ' SQL Code for seeing if last week's data exists.
strSQL = "SELECT * " & _
"FROM MainDataTable " & _
"WHERE MainDataTable.[ParentId] = " & ParentID & "AND MainDataTable.[InputDate] <# " & InputDate & "AND NOT MainDataTable.[Data] = 0
ORDER BY MainDataTable.[InputDate] DESC;"
Set rst = CurrentDb.OpenRecordset(strSQL): rst.MoveLast: rst.MoveFirst
rc = rst.RecordCount
If rc = 0 Then GoTo Cleanup 'if no record, then we are out of luck
If rc > 0 Then 'If there's some Record
Do Until rs.EOF = True Or GetLastWeek > 0
Dim price As Integer: price = rst("Data")
If price > 0 Then: GetLastWeek = price
rs.MoveNext
Loop
End If
Cleanup:
rst.Close
Set rst = Nothing
If GetLastWeek = 0 Then GetLastWeek = 1 '1 means no data was found
'Set so the output if nothing is found to 1 so that the code doesn't have to run on the same rows every single week
End Function
我不确定你所拥有的是如何工作的,但你所要做的就是遍历 parentid, date asc
订购的主 table,然后保存 parentID 和数据值只要它不为零,就会在下一次迭代 (Dim lDataValPrev as long
) 中使用。如果当前数据值为零且 parentID 未更改,请将其替换为您存储的先前值。您只需检查一次数据,无需额外调用。