L'errore nel tuo primo tentativo è che non puoi combinare la funzione aggregata count(*) con il non aggregato selezione di righe. Puoi risolvere questo problema usando count() come funzione window-aggregate invece:
SELECT * FROM (
SELECT *, ((row_number() OVER (ORDER BY "time"))
% ceil(count(*) OVER () / 500.0)::int) AS rn
FROM data_raw
) sub
WHERE sub.rn = 0; Spiegazione dettagliata qui:
@Alexander ha una soluzione per il tuo ultimo tentativo.