Below two versions of the same exactly query
Version 1 (uses k as alias in inner SELECT):
SELECT k, w_vol,
ROW_NUMBER
I don't have a specific explanation for the behavior, but note that both results are still correct: the only difference between the results is that a different order was chosen when sorting several rows that have the same value (1
) for the column being sorted (w_vol
).
Generally speaking, BigQuery does not promise any particular ordering when applying ORDER BY
to equal values, and that ordering may vary across different executions of the same query for any reason.
I agree it's interesting that the ordering seems to depend on the alias, but I could see plenty of plausible explanations for why this happens--for example, the engine might iterate through a list of fields that use analytic functions, and changing the alias might change the order in which those analytic functions are evaluated, which in turn changes the ordering of rows with equal values when sorting by one column.