1

These are my table columns:

 ID || Date || Description || Priority

My goal is to insert random test data of 2000 rows with date ranging between (7/1/2019 - 7/1/2020) and randomize the priority from list (High, Medium, Low).

I know how to insert random numbers but I am stuck with the date and the priority fields.

If I need to write code, any pointers on how do I do it?

Just want to be clear - I have issue with randomizing and inserting from a given list

James Z
  • 12,209
  • 10
  • 24
  • 44
smiley
  • 491
  • 3
  • 14
  • 36
  • Tag your question with the database you are using. Also, explain what "random" means in the context of "randomize the priority". – Gordon Linoff Jul 29 '20 at 01:27

2 Answers2

1
CREATE TABLE mytable (
  id SERIAL PRIMARY KEY,
  date DATE NOT NULL,
  description TEXT,
  priority ENUM('High','Medium','Low') NOT NULL
);

INSERT INTO mytable (date, priority)
  SELECT '2019-07-01' + INTERVAL FLOOR(RAND()*365) DAY, 
      ELT(1+FLOOR(RAND()*3), 'High', 'Medium', 'Low') 
  FROM DUAL;

The fake table DUAL is a special keyword. You can select from it, and it always returns exactly one row. But it has no real columns with data, so you can only select expressions.

Do this INSERT a few times and you get:

mysql> select * from mytable;                                                                                                                                       

+----+------------+-------------+----------+
| id | date       | description | priority |
+----+------------+-------------+----------+
|  1 | 2019-10-20 | NULL        | Medium   |
|  2 | 2020-05-17 | NULL        | High     |
|  3 | 2020-06-25 | NULL        | Low      |
|  4 | 2020-05-06 | NULL        | Medium   |
|  5 | 2019-09-30 | NULL        | High     |
|  6 | 2019-08-06 | NULL        | Low      |
|  7 | 2020-02-21 | NULL        | High     |
|  8 | 2019-11-10 | NULL        | High     |
|  9 | 2019-07-30 | NULL        | High     |
+----+------------+-------------+----------+

Here's a trick to use the number of rows in the table itself to insert the same number of rows, basically doubling the number of rows:

INSERT INTO mytable (date, priority)
  SELECT '2019-07-01' + INTERVAL FLOOR(RAND()*365) DAY, 
      ELT(1+FLOOR(RAND()*3), 'High', 'Medium', 'Low') 
  FROM mytable;

Just changing FROM DUAL to FROM mytable I change from selecting one row, to selecting the current number of rows from the table. But the values I insert are still random expressions, not the values already in those rows. So I get new rows with new random values.

Then repeat this INSERT as many times as you want to double the number of rows.

Read also about the ELT() function.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • You can combine this answer with the following answer, which shows how to generate sequential numbers and join. You can use it to multiply the SELECT. This way you can insert multiple records at once. https://stackoverflow.com/questions/304461/generate-an-integer-sequence-in-mysql – K4M Jul 29 '20 at 03:01
  • I wrote a stored procedure to loop through and it took about 0.00076 sec. Thanks for all your help! – smiley Jul 29 '20 at 04:51
-1

You seem to be looking for something like this. A basic random sample is:

select t.*
from t
where date >= '2019-07-01' and date < '2020-07-01'
order by random()
fetch first 2000 rows only;

Of course, the function for random() varies by database, as does the logic for limiting rows. This should get about the same distribution of priorities as in the original data.

If you want the rows to come by priority first, then use:

select t.*
from t
where date >= '2019-07-01' and date < '2020-07-01'
order by (case when priority = 'High' then 1 when priority = 'Medium' then 2 else 3 end),
         random()
fetch first 2000 rows only;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Given a data-list {High, Medium, Low} I need to randomize this list and insert them as 2000 rows. Issue is how do I insert the data. Also the data range is given and I need to randomize date list and insert as 2000 rows – smiley Jul 29 '20 at 01:31
  • @smiley . . . You insert data into a table using `insert`. – Gordon Linoff Jul 29 '20 at 01:35