Isaac
Lifelong Learner
- Local time
- Today, 12:46
- Joined
- Mar 14, 2017
- Messages
- 11,111
I'm trying to delete some records from a large table, a one time clean-up operation, and having some unexpected results. Wondering whether there is something I'm missing that anybody might identify!
Large table, ~half a million records: TableMain. Among many other fields, it has sp_id and [bu]
Small table, TableMapping, intended to be a mapping table that shows the ONLY "allowed" combinations of fields [sp_id] and [bu]. This table has one record per distinct combination of [sp_id] and [bu]........and also, one record per [bu]. So there can be multiple bu's assigned to one sp_id, so you might see a few records that look like this:
sp_id | bu
001 | buname1
001 | buname2
002 | buname3
002 | buname4
which means that buname1 should ONLY belong to sp_id 001, and buname3 should ONLY belong to sp_id 002
However, in my TableMain, it is a mess. We have records where buname1 occurs for numerous different sp_id's - even though the only records we want to preserve are those where bu=buname1 and sp_id=001
So here is my code:
That should take care of cases where TableMain actually has records with the same BU from TableMapping, BUT, the sp_id was not the correct one.
Now, in case TableMain has records with a totally unknown BU--one that didn't even exist in TableMapping, I want to eliminate all those too:
At the end of all this, as expected, TableMain is much smaller. Instead of half a million records, it has about 6,000.
However, this process seems to have eliminated too many records.
For example: The following record was eliminated: Where the BU was xxx and the sp_id was yyy, even though that combination of BU/sp_id (xxx/yyy) WAS on the TableMapping, and was an allowable combination.
What have I screwed up here??
I'm about to do some more 'stepping' than I already have, but figured I'd put it out here in case anyone noticed anything logically flawed too...
Large table, ~half a million records: TableMain. Among many other fields, it has sp_id and [bu]
Small table, TableMapping, intended to be a mapping table that shows the ONLY "allowed" combinations of fields [sp_id] and [bu]. This table has one record per distinct combination of [sp_id] and [bu]........and also, one record per [bu]. So there can be multiple bu's assigned to one sp_id, so you might see a few records that look like this:
sp_id | bu
001 | buname1
001 | buname2
002 | buname3
002 | buname4
which means that buname1 should ONLY belong to sp_id 001, and buname3 should ONLY belong to sp_id 002
However, in my TableMain, it is a mess. We have records where buname1 occurs for numerous different sp_id's - even though the only records we want to preserve are those where bu=buname1 and sp_id=001
So here is my code:
Code:
Sub DeleteJunkFromMainTable()
'out of the >half million records imported from the combination of all
Dim rs As DAO.Recordset, db As DAO.Database, strID As String, strBU As String, strSQL As String, strIn As String
Set db = CurrentDb
Set rs = db.OpenRecordset("select DISTINCT [BU],[SP_ID] from TableMapping")
Do Until rs.EOF = True
strBU = Replace(rs.Fields("BU").Value, "'", "''")
strID = rs.Fields("SP_ID").Value
'delete all records from TableMain where the BU equals this particular mapping table BU, and the sp_id is NOT the proper one:
strSQL = "delete * from TableMain where BU='" & strBU & "' and sp_id<>" & strID
CurrentDb.Execute strSQL, dbFailOnError
'Debug.Print strSQL
rs.MoveNext
Loop
End Sub
That should take care of cases where TableMain actually has records with the same BU from TableMapping, BUT, the sp_id was not the correct one.
Now, in case TableMain has records with a totally unknown BU--one that didn't even exist in TableMapping, I want to eliminate all those too:
Code:
Currentdb.Execute "delete * from TableMain where BU not in(select bu from TableMapping)"
At the end of all this, as expected, TableMain is much smaller. Instead of half a million records, it has about 6,000.
However, this process seems to have eliminated too many records.
For example: The following record was eliminated: Where the BU was xxx and the sp_id was yyy, even though that combination of BU/sp_id (xxx/yyy) WAS on the TableMapping, and was an allowable combination.
What have I screwed up here??
I'm about to do some more 'stepping' than I already have, but figured I'd put it out here in case anyone noticed anything logically flawed too...
Last edited: