Update column based on another column in table - MS Access

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Update column based on another column in table - MS Access



I have a table, titled Game, structured like this:


[Game_Name] [Publisher]



The data in the table would look like this:


[Game_Name] [Publisher]
1
3
1
2
2
1
3



And Game_Name will be blank. With this, I want to update the value of Game_Name based on the value of the Publisher column.



How this should work:



If the Publisher = 1 -> for each of the instances (rows) where the Publisher column is 1, the Game_Name will be GM-1,...GM-n. So, in this example, there are 3 instances where Publisher is 1. So the values would be GM-1, GM-2, GM-3.



If the publisher = 2, similar to the above, expect the format is 1-1,...n-n. So, since we have 2 instances where Publisher is 2, the values for Game_Name would be GM-1-1, GM-2-2.



Lastly, if Publisher = 3, the values for Game_Name would be 1-1-1,...n-n-n. There are three instances here so the values for Game_Name would be GM-1-1-1, GM-2-2-2, GM-3-3-3.



This is my current approach


UPDATE Game
SET Game_Name =
SWITCH (Publisher = 1, "GM-"&[1+1],
Publisher = 2, "GM-"&[1+1]&"-"&[1+1],
Publisher = 3, "GM-"&[1+1]&"-"&[1+1]&"-"&[1+1])



I am having trouble keeping track of the incrementing value and when to reset. Also, whether this should be a Macro or not.



Any help is appreciated.





As records have no order - unless specified, and that's not the case - use Excel for this.
– Gustav
Aug 7 at 13:23





Hm. Good point. Do you have any recommendations as to how I could structure this in Access to meet the goal?
– Darnold14
Aug 7 at 13:30





Add an AutoNumber field with sequential numbering.
– Gustav
Aug 7 at 13:41





Hm ok, but if I had, say 7 examples. Then I would have an ID column that goes 1-7. How could that help me?
– Darnold14
Aug 7 at 13:43





You can sort on that. You could also use a date field with date/time for the game.
– Gustav
Aug 7 at 15:40




1 Answer
1



Consider:


SELECT ID, Publisher, "GM-" & Choose([Publisher],[GrpSeq],[GrpSeq] & "-" & [GrpSeq],[GrpSeq] & "-" & [GrpSeq] & "-" & [GrpSeq]) AS Game_Name
FROM (SELECT ID, Publisher, DCount("*","Game","Publisher = " & [Publisher] & " AND ID <" & [ID])+1 AS GrpSeq FROM Game) AS Q1;



Be aware this will likely perform slowly with very large dataset. In which case a VBA approach would probably be preferable. If there are significantly more than 3 Publisher values, VBA approach may be only option.





This is really impressive. Thanks. I just need to insert these values into the table.
– Darnold14
Aug 7 at 19:48






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS