~/ learn/ postgresql/ cards/ NTILE, FIRST_VALUE & frames
1 of 2

Split the books into 4 price quartiles with NTILE. Columns: title, price, quartile. Order by price.

Split the books into 4 price quartiles with NTILE. Columns: title, price, quartile. Order by price.

Answer

SELECT title, price, ntile(4) OVER (ORDER BY price) AS quartile FROM books ORDER BY price;

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/7bc9938f-4c41-4a71-8b9f-bd20fbe13c43/flashcard utf-8 LF