Quartiles in SQL query

前端 未结 6 2106
面向向阳花
面向向阳花 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条回答
  •  旧时难觅i
    2020-12-11 08:07

    Something like this should do it:

    select
        ll.*,
        if (a.position is not null, 1,
            if (b.position is not null, 2, 
            if (c.position is not null, 3, 
            if (d.position is not null, 4, 0)))
        ) as quartile
    from
        luxlog ll
        left outer join luxlog a on ll.position = a.position and a.lux > (select count(*)*0.00 from luxlog) and a.lux <= (select count(*)*0.25 from luxlog)
        left outer join luxlog b on ll.position = b.position and b.lux > (select count(*)*0.25 from luxlog) and b.lux <= (select count(*)*0.50 from luxlog)
        left outer join luxlog c on ll.position = c.position and c.lux > (select count(*)*0.50 from luxlog) and c.lux <= (select count(*)*0.75 from luxlog)
        left outer join luxlog d on ll.position = d.position and d.lux > (select count(*)*0.75 from luxlog)
    ;    
    

    Here's the complete example:

    use example;
    
    drop table if exists luxlog;
    
    CREATE TABLE LuxLog (
      Sensor TINYINT,
      Lux INT,
      position int,
      PRIMARY KEY(Position)
    );
    
    insert into luxlog values (0, 1, 10);
    insert into luxlog values (0, 2, 20);
    insert into luxlog values (0, 3, 30);
    insert into luxlog values (0, 4, 40);
    insert into luxlog values (0, 5, 50);
    insert into luxlog values (0, 6, 60);
    insert into luxlog values (0, 7, 70);
    insert into luxlog values (0, 8, 80);
    
    select count(*)*.25 from luxlog;
    select count(*)*.50 from luxlog;
    
    select
        ll.*,
        a.position,
        b.position,
        if(
            a.position is not null, 1,
            if (b.position is not null, 2, 0)
        ) as quartile
    from
        luxlog ll
        left outer join luxlog a on ll.position = a.position and a.lux >= (select count(*)*0.00 from luxlog) and a.lux < (select count(*)*0.25 from luxlog)
        left outer join luxlog b on ll.position = b.position and b.lux >= (select count(*)*0.25 from luxlog) and b.lux < (select count(*)*0.50 from luxlog)
        left outer join luxlog c on ll.position = c.position and c.lux >= (select count(*)*0.50 from luxlog) and c.lux < (select count(*)*0.75 from luxlog)
        left outer join luxlog d on ll.position = d.position and d.lux >= (select count(*)*0.75 from luxlog) and d.lux < (select count(*)*1.00 from luxlog)
    ;    
    
    
    select
        ll.*,
        if (a.position is not null, 1,
            if (b.position is not null, 2, 
            if (c.position is not null, 3, 
            if (d.position is not null, 4, 0)))
        ) as quartile
    from
        luxlog ll
        left outer join luxlog a on ll.position = a.position and a.lux > (select count(*)*0.00 from luxlog) and a.lux <= (select count(*)*0.25 from luxlog)
        left outer join luxlog b on ll.position = b.position and b.lux > (select count(*)*0.25 from luxlog) and b.lux <= (select count(*)*0.50 from luxlog)
        left outer join luxlog c on ll.position = c.position and c.lux > (select count(*)*0.50 from luxlog) and c.lux <= (select count(*)*0.75 from luxlog)
        left outer join luxlog d on ll.position = d.position and d.lux > (select count(*)*0.75 from luxlog)
    ;    
    

提交回复
热议问题