jeudi 11 août 2022

SQL Update Query in ACCESS: Generate individual random numbers within an interval for each record in a group

I want to generate some random values for a column (PreisLager) in a table (tblProdukte) using an Update query in MS ACCESS. I want the update query to populate column fields if they are NULL only. I want the updated values to be randomly generated within some interval. I want that Interval to differ by grouping (GroupID). And I want the random numbers to be drawn individually for each item in a group in sequence (or using different starting seeds for each item, not sure how the Rnd function works internally), meaning all records in the group get potentially different random numbers all drawn using the same group based interval. So, the interval for random number generation differs between groups, but not within them. Each item within a group receives a different draw, using the same interval. I have tried out some stuff using SQL but I am only able to get the interval be different between groups. The random numbers within those groups are all identical as I cannot figure out a way to use SQL to randomly "re-draw" the number each record in a group. I am imagining some type of group based for loop, not sure if that can be done in SQL, I have not really been using it much at all. Obviously I could group by individual recordID, but that would be a colossal mess. So far I was not able to find a way to do it in SQL. Ideally I would like to do it in SQL though. If that is not possible maybe there is a way using vba after the fact?? I am not looking for external libraries or other third party content, just basic ms access.

Here the code I have so far, which, as I mentioned, does not function the way I would like. If there is a cleaner way to write it that would also be welcome since it is a bit messy looking.

UPDATE tblProdukte
SET PreisLager = SWITCH(
[GroupID]=1, Int ((130 - 55 + 1) * Rnd + 55),
[GroupID]=2, Int ((650 - 400 + 1) * Rnd + 400), 
[GroupID]=3, Int ((160 - 88 + 1) * Rnd + 88), 
[GroupID]=4, Int ((850 - 550 + 1) * Rnd + 550), 
[GroupID]=5, Int ((950 - 600 + 1) * Rnd + 600), 
[GroupID]=6, Int ((1200 - 750 + 1) * Rnd + 750), 
[GroupID]=7, Int ((300 - 80 + 1) * Rnd + 80),
[GroupID]=9, Int ((200 - 25 + 1) * Rnd + 25))
WHERE (((tblProdukte.PreisLager) Is Null));



Aucun commentaire:

Enregistrer un commentaire