vendredi 2 décembre 2022

BigQuery Distinct reorders random ordering

I want to shuffle my query results randomly using ORDER BY RAND(), but when using DISTINCT the ordering statement seems to be ignored.

Code to reproduce:

WITH tmp AS (
SELECT 'aa' cardno
UNION ALL
SELECT 'bb' cardno
UNION ALL
SELECT 'cc' cardno
UNION ALL
SELECT 'dd' cardno
UNION ALL
SELECT 'ee' cardno
UNION ALL
SELECT 'ff' cardno
UNION ALL
SELECT 'gg' cardno
UNION ALL
SELECT 'hh' cardno
)

SELECT DISTINCT cardno FROM
(
SELECT *, RAND() rnd FROM tmp
ORDER BY 2
)

The results are ordered alphabetically, not random as expected. Im using Google cloud platform BigQuery




Aucun commentaire:

Enregistrer un commentaire