Because SQLite sometimes evaluates functions like random()
more than once if they appear in a subquery, which may be a bug, eg:
select op, op from (select abs(random())%10 as op from (select 1));
3|1
I need an easy way to force SQLite to materialize the subquery so that random()
is only evaluated once
this is a simplified example for demonstration – my real world query does not have from (select 1)
but selects from a view
Best Answer
From the SQLite docs (my emphasis):
You may be able to work around the problem with a view, but it seems that in this case at least, adding a dummy
union
is enough to prevent "flattening":