SQLite Forum

Bug: inconsistent result when an optimization is on and off
Login
I believe that the documentation at
<https://sqlite.org/datatype3.html#affcompoundview> applies in this case.
In other words, this is the same (non-)issue that you reported
previously at [forum post 02d7be94d7](/forumpost/02d7be94d7).

You have a compound sub-query in which the output column sometimes
has TEXT affinity and other times has INTEGER affinity.  So the affinity
of the C2 column in the overall sub-query is indeterminate.  According
to [the documentation][1]:

> Different constituent SELECT statements might be used to determine affinity at different times during query evaluation. The choice might vary across different versions of SQLite. The choice might change between one query and the next in the same version of SQLite. The choice might be different at different times within the same query. Hence, you can never be sure what affinity will be used for columns of a compound SELECT that have different affinities in the constituent subqueries.<p>
Best practice is to avoid mixing affinities in a compound SELECT if you care about the datatype of the result. Mixing affinities in a compound SELECT can lead to surprising and unintuitive results.\

[1]: https://sqlite.org/datatype3.html#affcompoundview