I came upon an interesting behaviour when I use RAND() within WITH:
WITH
t0 AS ( SELECT rand() AS c ),
t1 AS ( SELECT c FROM t0 ),
t2 AS ( SELECT c FROM t0 )
SELECT c FROM t1
UNION ALL
SELECT c FROM t2
--
-- returns:
-- 1 0.229810680294245
-- 2 0.5750437237982657
-- instead of:
-- 1 0.229810680294245
-- 2 0.229810680294245
--
The same thing even happens when I do this:
WITH
t0 AS ( SELECT rand() AS c )
SELECT c FROM t0
UNION ALL
SELECT c FROM t0
Any clues why that happens?
Surfacing this reply from Elliott Brossard as a comment to a deleted answer:
RAND()
is supposed to run only once when used in a WITH clause. WITH clauses are similar to macros--they are inlined into the query plan--and this is a known bug in that RAND() will be evaluated using different seeds when run on different tasks, but the fix is somewhat complicated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With