I have a table named POS_File_Detail. I would like to update Line_Number based on the count of the Invoice_Number and increment by 1 for every duplicate. So basically if I have three duplicate entries with the same Invoice_Number it will update Line_Number with what duplicate number it is. Something like this
Invoice_Number | Line_Number | Description
0123456 1 Pepsi
0123456 2 Chips
0123499 1 Hot dogs
0123456 3 Mustard
The primary key for this table is an autonumber field named Record_ID.
I’m not sure how to put this in correct syntax, but hopefully this will show you what I’m trying to do
UPDATE POS_File_Detail
SET POS_File_Detail.Line_Number = Line_Number +1
WHERE COUNT(Invoice_Number)>1;
This can be really easy if you have a field or combination of fields which allow you to define the ordering of the duplicate Invoice_Number values. Without such a method, the order in which those Invoice_Number values are retrieved is arbitrary.
You have an autonumber field named “Record_ID” as POS_File_Detail primary key.
Although you asked to store Line_Number in POS_File_Detail, I gave you a SELECT statement instead. I did that because it’s almost always a mistake to store derived values in the table. Better to derive Line_Number with a query whenever you need it. That way you avoid storing redundant data and ensure Line_Number is always current despite inserts, updates and deletions to POS_File_Detail.
However if you have a sound reason to actually store Line_Number in POS_File_Detail, you can convert the SELECT to an UPDATE statement.
Please make sure you really have a compelling reason to store those values.