将记录分组并放入访问中的不同表中
Group records and put into different tables in access
我有一个包含两个 table 的数据库。 (每个 table 中有 5,034,139 和 3,297,127 行)在这些 table 中有报告名称(~1000 种不同的报告)和日志日期。我想为每个报告名称创建新的 table,每个 table 应该包含报告的日志日期。
假设只有两个报告 "a" 和 "b"。报告 "a" 的日志日期是 01.01.2014、03.04.2014 和 05.06.2014。报告 "b" 的日志日期为 04.07.2013、06.08.2014 和 02.03.2014。
我想创建两个 tables/查询。在一个 table / 查询中应该只有报告 a 的日志日期,而在另一个 table 中应该只有报告 b 的日志日期。像查询 1 的 SELECT * FROM Table1
AND SELECT * FROM Table2 WHERE REPORT = "a"
和查询 2 的 SELECT * FROM Table1
AND SELECT * FROM Table2 WHERE REPORT = "b"
但是一千多个report name我都要做这个
好的 - 这实际上有点复杂,但您要做的是创建一个循环,通过 Report Name 循环遍历您的记录(以获取名称来创建tables) 然后执行 Select Into 以创建一个新的 table 并包含每个报告的记录。
你应该得到这样的结果:
Dim rs As DAO.Recordset
Dim strSQL as string
Dim ReportName as string
Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT ReportName FROM TABLE1 UNION SELECT DISTINCT ReportName FROM TABLE2 ")
docmd.SetWarnings false
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
ReportName = rs("ReportName") 'GET THE REPORTNAME
strSQL="SELECT LogDates INTO " & ReportName & _
"FROM (SELECT LogDates FROM Table1 " & _
"WHERE ReportName = '" & ReportName & "' " &
"UNION "
"SELECT LogDates FROM Table1 " & _
"WHERE ReportName = '" & ReportName & "') " &
DoCmd.RunSql strSQL
Docmd.setwarnings True
'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
MsgBox "Finished looping through records."
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
您必须使用您的 table 和字段名称,但这应该为您提供所需操作的基础知识。
我有一个包含两个 table 的数据库。 (每个 table 中有 5,034,139 和 3,297,127 行)在这些 table 中有报告名称(~1000 种不同的报告)和日志日期。我想为每个报告名称创建新的 table,每个 table 应该包含报告的日志日期。
假设只有两个报告 "a" 和 "b"。报告 "a" 的日志日期是 01.01.2014、03.04.2014 和 05.06.2014。报告 "b" 的日志日期为 04.07.2013、06.08.2014 和 02.03.2014。
我想创建两个 tables/查询。在一个 table / 查询中应该只有报告 a 的日志日期,而在另一个 table 中应该只有报告 b 的日志日期。像查询 1 的 SELECT * FROM Table1
AND SELECT * FROM Table2 WHERE REPORT = "a"
和查询 2 的 SELECT * FROM Table1
AND SELECT * FROM Table2 WHERE REPORT = "b"
但是一千多个report name我都要做这个
好的 - 这实际上有点复杂,但您要做的是创建一个循环,通过 Report Name 循环遍历您的记录(以获取名称来创建tables) 然后执行 Select Into 以创建一个新的 table 并包含每个报告的记录。
你应该得到这样的结果:
Dim rs As DAO.Recordset
Dim strSQL as string
Dim ReportName as string
Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT ReportName FROM TABLE1 UNION SELECT DISTINCT ReportName FROM TABLE2 ")
docmd.SetWarnings false
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
ReportName = rs("ReportName") 'GET THE REPORTNAME
strSQL="SELECT LogDates INTO " & ReportName & _
"FROM (SELECT LogDates FROM Table1 " & _
"WHERE ReportName = '" & ReportName & "' " &
"UNION "
"SELECT LogDates FROM Table1 " & _
"WHERE ReportName = '" & ReportName & "') " &
DoCmd.RunSql strSQL
Docmd.setwarnings True
'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
MsgBox "Finished looping through records."
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
您必须使用您的 table 和字段名称,但这应该为您提供所需操作的基础知识。