Using random() in update produces same random value for all

From: Alex Magnum <magnum11200(at)gmail(dot)com>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Using random() in update produces same random value for all
Date: 2018-01-14 10:01:11
Message-ID: CA+cR4zde6SSaMfznJRgrM1zB2NYHjh6Uzn1ARXYW9_4sRvvUPA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,
i am trying to update a table with some random dates but that does not seem
to work.

UPDATE table
SET last_update=now()::date-(SELECT (random() * 5)::INTEGER + 1)

The updated field is always set to the same. Is there a way to make it
random for every record?

I could run it through a function but I wonder if there is s simpler way.

Thanks for any help on this

Alex

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ken Tanzer 2018-01-14 10:24:04 Re: Using random() in update produces same random value for all
Previous Message Tom Lane 2018-01-13 23:12:58 Re: psql format result as markdown tables