Quartiles in SQL query

前端 未结 6 2076
面向向阳花
面向向阳花 2020-12-11 07:44

I have a very simple table like that:

CREATE TABLE IF NOT EXISTS LuxLog (
  Sensor TINYINT,
  Lux INT,
  PRIMARY KEY(Sensor)
)

It contains

6条回答
  •  無奈伤痛
    2020-12-11 08:09

    Well to use NTILE is very simple but it is a Postgres Function. You basically just do something like this:

    SELECT value_you_are_NTILING,
        NTILE(4) OVER (ORDER BY value_you_are_NTILING DESC) AS tiles
    FROM
    (SELECT math_that_gives_you_the_value_you_are_NTILING_here AS value_you_are_NTILING FROM tablename);
    

    Here is a simple example I made for you on SQLFiddle: http://sqlfiddle.com/#!15/7f05a/1

    In MySQL you would use RANK... Here is the SQLFiddle for that: http://www.sqlfiddle.com/#!2/d5587/1 (this comes from the Question linked below)

    This use of MySQL RANK() comes from the Stackoverflow answered here: Rank function in MySQL

    Look for the answer by Salman A.

提交回复
热议问题