mardi 24 janvier 2017

How do I update a column with a random value seeded from another column?

I have a table with some duplicate dates, and I need to add a column that stores a random value obtained with RAND(), so that rows with the same date get different random values (the column is added DEFAULT NULL since this is a data migration; also let's ignore type conversions and assume RAND() can take a date column).

I want a result like this (notice how values in Rndm is different for the two rows with date 2017-01-01):

Date       | Rndm
---------- | -----
2017-01-01 | 0.34
2017-01-02 | 0.15
2017-01-01 | 0.78

That is, given a certain seed, the rows should get the values of the series generated starting from that seed (order or rows for a certain date is not important).

If I simpy do:

UPDATE SET rndm=RAND(Date) WHERE Rndm IS NULL

I get instead something like this:

Date       | Rndm
---------- | ----
2017-01-01 | 0.34
2017-01-02 | 0.15
2017-01-01 | 0.34

That is, I get the first value in the random series generated from seed for all the rows with the same date.

I cannot come up with a SQL statement that does that. Do I need to iterate over the distinct() Dates?




Aucun commentaire:

Enregistrer un commentaire