Renumbering Module (1 Viewer)

Snowflake68

Registered User.
Local time
Today, 11:58
Joined
May 28, 2014
Messages
452
I have a table which has two fields with a stock code multiple times together with an operation number in ascending order for each of the lines. I then need to generate a new number in the 'Final Op' field incremented by 1 up to the last line for that stock code and then start again at 1 for the next stock code and so on. I have hundreds of stock codes so doing this manually is not an option plus I need to automate this so someone can run it again in the future.

I dont know how to write the code myself so I wondered if someone could write the code for me please. I would be extremely grateful.

For example
StockCode OperationNumber FinalOp
100000 201 1
100000 202 2
100000 203 3
100000 204 4
100000 205 5
100001 201 1
100001 202 2
100001 203 3
100001 204 4
100001 205 5
100001 206 6
100001 301 7
100002 201 1
100002 202 2
100002 203 3
100002 204 4
100002 205 5
100002 206 6
100002 301 7
100002 302 8


Thanks in advance.
Attached is the full table I have.
 

Attachments

  • Export for Renumbering.zip
    114.9 KB · Views: 52

CJ_London

Super Moderator
Staff member
Local time
Today, 11:58
Joined
Feb 19, 2013
Messages
16,610
in sql you would have something like

Code:
SELECT A.StockCode, A.OperationNumber, Count(B.OperationNo) AS FinalOp
FROM myTable A INNER JOIN myTable B ON A.StockCode=B.stockcode
WHERE B.OperationNumber<=A.OperationNumber
not the use of aliases for the tables
 

Snowflake68

Registered User.
Local time
Today, 11:58
Joined
May 28, 2014
Messages
452
Hi CJ_London

I have tried your suggestion but I am getting an error that I dont understand.
This is my SQL as my table is called OperationRenumber

Code:
SELECT A.StockCode, A.OperationNumber, Count(B.OperationNo) AS FinalOp
FROM OperationRenumber AS A INNER JOIN OperationRenumber AS B ON A.StockCode = B.stockcode
WHERE (((B.OperationNumber)<=[A].[OperationNumber]));

The error I get is "Your query does not include the specified expression 'StockCode' as part of an aggregate function"

The table does have the field name of 'StockCode' so I dont understand why I am getting this error.

Attached is a screenshot of the query design in Access.

Once I get this query working I want to be able to change it into an update query (or a make table one) so that I have the 'FinalOp' with the new values.

Hope you can help
 

Attachments

  • query.JPG
    query.JPG
    41.9 KB · Views: 54

CJ_London

Super Moderator
Staff member
Local time
Today, 11:58
Joined
Feb 19, 2013
Messages
16,610
Ah sorry, forgot the GROUP BY

Code:
SELECT A.StockCode, A.OperationNumber, Count(B.OperationNo) AS FinalOp
FROM OperationRenumber AS A INNER JOIN OperationRenumber AS B ON A.StockCode = B.stockcode
WHERE (((B.OperationNumber)<=[A].[OperationNumber]))
GROUP BY A.StockCode, A.OperationNumber;
 

CJ_London

Super Moderator
Staff member
Local time
Today, 11:58
Joined
Feb 19, 2013
Messages
16,610
suspect you cannot use this as an update query - and there should be no need to store the value, just calculate as and when required.
 

Snowflake68

Registered User.
Local time
Today, 11:58
Joined
May 28, 2014
Messages
452
Ah sorry, forgot the GROUP BY

Code:
SELECT A.StockCode, A.OperationNumber, Count(B.OperationNo) AS FinalOp
FROM OperationRenumber AS A INNER JOIN OperationRenumber AS B ON A.StockCode = B.stockcode
WHERE (((B.OperationNumber)<=[A].[OperationNumber]))
GROUP BY A.StockCode, A.OperationNumber;


Thank you so much this works perfectly.

I had to make a slight adjustment to the field name OperationNo as it called OperationNumber but otherwise it works great.

Here is my final code

Code:
SELECT A.StockCode, A.OperationNumber, Count(B.OperationNumber) AS FinalOp
FROM OperationRenumber AS A INNER JOIN OperationRenumber AS B ON A.StockCode = B.stockcode
WHERE (((B.OperationNumber)<=[A].[OperationNumber]))
GROUP BY A.StockCode, A.OperationNumber;
 

CJ_London

Super Moderator
Staff member
Local time
Today, 11:58
Joined
Feb 19, 2013
Messages
16,610
If you have a large number of records, ensure that StockCode and OperationNumber fields are both indexed, otherwise it will be pretty slow
 

Users who are viewing this thread

Top Bottom