Insert random values in Fact Table
Hello,
I'm trying to fill a table with values. It should pick foreign keys from the tables at random.
What happens is that every row inserted is exactly the same.
Thanks for your support.
`
insert into sales (sold_units, total_sales_amount, calendar_date_id, location_location_id, product_product_id, organization_organization_id)
select
(1 + RANDOMINT(100)) AS sold_units,
(10000 + RANDOM()*50000)::NUMERIC(7,2) AS total_sales_amount, (
select dt_id from calendar group by dt_id having dt_id>=(randomint(max(dt_id)-min(dt_id)) + min(dt_id)) limit 1) as calendar_date_id,(
select location_id from location order by random() limit 1) as location_location_id,(
select organization_id from organization order by random() limit 1) as organization_organization_id,(
select product_id from product order by random() limit 1) as product_product_id
FROM ( SELECT tm FROM ( SELECT now() + INTERVAL '1 second' AS t UNION ALL SELECT now() + INTERVAL '1000000 seconds' AS t ) x TIMESERIES tm AS '1 second' OVER(ORDER BY t) ) y
;
COMMIT;
`
Answers
The fullselects used as entries in the select list are only evaluated once in the query plan, and the resulting values are then used for all rows that the query produces.
Works as designed, I'm afraid.
There is a better approach - at least I think there is.
If you use
RANDOM() and similar to produce the fact table rows, there's a high probability that you also use that technique for the generation of the foreign keys. For example, if you generated 10,000 product dimension rows, and used 10,000 consecutive integers, starting with 1 for the product ID, use
RANDOMINT(10000)+1` for the product foreign key in the fact table - I hope that makes sense ...Does sample data use make sense here?
See: https://www.vertica.com/docs/latest/HTML/Content/Authoring/AnalyzingData/MachineLearning/DataPreparation/SamplingData.htm