DEV Community

Discussion on: Challenge: Get Closest Number in an Array

Collapse
 
dmfay profile image
Dian Fay

Window functions!

SELECT unnest
FROM unnest(ARRAY[100,200,400,800,1600,3200,6400,128000])
ORDER BY row_number() OVER (ORDER BY abs(900 - unnest))
LIMIT 1;
Collapse
 
rhymes profile image
rhymes

hahaha thinking outside the box :D

Collapse
 
dmfay profile image
Dian Fay

I looked at it again just now and the row_number is redundant anyway...

SELECT unnest
FROM unnest(ARRAY[100,200,400,800,1600,3200,6400,128000])
ORDER BY abs(900 - unnest)
LIMIT 1;