2

There is a PostgreSQL SQL SELECT results. I need to divide rows into quintiles and update quintil value to the specific row.

Is there some possibility to do this requirement in SELECT without need to do it in application? I would like to avoid situation when I need to select data to application and do the ranking out of PostgreSQL server.

Data example - first column is value, second column is quintil

4859 - 5
4569 - 5
4125 - 4
3986 - 4
3852 - 3
3562 - 3
3452 - 2
3269 - 2
3168 - 1
3058 - 1

Thank you.

4

1 に答える 1

8

これを生成するための「ntile」と呼ばれるウィンドウ関数があります。これを生成する出力を分割する「タイル」の数(この場合は5)を指定するパラメーターを指定します。

例えば:

select t.id, ntile(5) over (order by t.id)
from t

ウィンドウ関数の概要についてはウィンドウ関数のチュートリアルを、提供されている標準機能のリストについてはウィンドウ関数を参照してください。

于 2012-11-20T11:03:33.573 に答える