Suppose you want to create some test data in a table where a column value should increase by a random percentage each row.
For example, you wish to generate “sales” data for a month. For each day of the month you want to record how many sales orders are in the system. Since sales orders are always added, the number of sales orders will continually increase. On some days it will increase more than others. To reflect this variable growth, I came up with this solution.
The sales table then has two columns: sales_date and number_of_orders.
CREATE TABLE sales (sales_date DATE,number_of_orders NUMBER);
The following SQL can generate ever increasing values for the number_of_orders column for a period of n days. The SQL takes three arguments:
- start_value – the number of sales orders you want to start out with
- Num_days – the number of days for which you want to generate data
- Start_date – the date from which you want to start generating data
- max_growth_pct – the maximum percentage you want to allow the number of sales order to grow.
For each row in the result set, the number of sales orders generated will have increased by a random percentage (within the given range). The pct_increase column shows by what percentage each day the number of sales orders have grown.
WITH a AS
(SELECT LEVEL lvl
(SUM(round(dbms_random.value(1, &max_growth_pct) * 10)) over(ORDER BY LEVEL)) new_val
CONNECT BY LEVEL <= &num_days)
SELECT to_date(‘&start_date’,’dd-mon-yy’) + lvl dt
,ROUND(new_val / GREATEST(LAG(new_val, 1, 0) over(ORDER BY lvl),&start_value)-1,4)*100 pct_increase
I used the following variable values to generate the rows below:
- start_value = 1000
- max_growth_pct = 10
- num_days = 10
- start_date = ’01-JAN-10′
Now all you have to do is wrap the SQL inside an insert statement and you’re ready to go.