I have two tables as following.
‘Areas’ Table
| AreaKey | AreaID |
|-----------------|--------------|
| <identity/int> | <varchar> |
‘Readings’ Table
| ReadingKey | AreaKey | Reading | ReadingDateTime |
|-----------------|-------------------|-------------|----------------------|
| <identity/int> |<FK:AreaKey-Areas> | <float> | <datetime> |
- ‘AreaKey’ in Readings table is a foreign key to ‘AreaKey’ of Areas table.
Areas table already have some data with row id’s ranging from 1 to 50.
I want to populate the Readings table with some sample data – (random float values for ‘Reading’ column between 1.0 and 100.0 AND a random datetime value for ReadingDateTime between a given DateTime range; for example between current datetime and a datetime 3 months back). These values should be inserted to the Reading table by randomly selecting AreaKeys which already exist in the Areas table.
In other words I want to insert random reading values to randomly selected areas, with random datetimes.
Can anyone give me a clue on how to do this?
Assuming your Areas table has 50 records, with row ids 1-50, I would just look into using the
RANDfunction.Something like this seems to work:
And here’s some SQL Fiddle.
Good luck.