MySQL查找获得最高积极进展所需的天数

问题描述 投票:0回答:2

数据集:

CREATE TABLE TEMPERATURES (
  CREATED DATE,
  ACTUAL_TEMPERATURE FLOAT
  );

INSERT INTO TEMPERATURES VALUES
('20170101',12.2),
('20170103',10.1),
('20170112',14.2),
('20170115',12.5),
('20170205',20.8),
('20170122',16.7),
('20170123',7.8),
('20170130',12.5),
('20170201',13.7),
('20170302',14.8),
('20170313',11.1),
('20170414',12.0),
('20170525',10.4);

和SQL FIDDLE相同:http://sqlfiddle.com/#!9/9b11e

我只想找到获得最高积极进展所需的天数?

我想要的结果是:

20170103 10.1
20170205 20.8

小心,我不想要:

20170123 7.8
20170302 14.8

我尝试了许多请求,但尝试了很多方法而没有成功

是否有可能,如果可以,怎么样?

谢谢你的帮助。

mysql sql select
2个回答
0
投票

根据您对问题的描述,我认为您需要:

select grp, min(created), max(created),
       min(actual_temperature), max(actual_temperature)
from (select t.*,
             (@grp := @grp + coalesce(prev_at > actual_temperature, 0)) as grp
      from (select t.*,
                   (select t2.actual_temperature
                    from temperatures t2
                    where t2.created < t.created
                    order by t2.created desc
                    limit 1
                   ) as prev_at
            from temperatures t
            order by created
           ) t cross join
           (select @grp := 0) params
      ) t
group by grp
order by max(actual_temperature) - min(actual_temperature) desc
limit 1;

这确定了温度升高的条纹。然后它返回最大值和最小值之间最大差异的条纹。

这就是我解释你的问题的方法,尽管你的结果并不完全符合这种解释。


0
投票

也许你可以使用这样的函数(你还需要调整边缘情况):

DELIMITER $$
CREATE FUNCTION find_progressive(startingDate DATE)
  RETURNS VARCHAR(100) DETERMINISTIC
  BEGIN
    DECLARE risultato int default 0;

    /* current cursor*/
     DECLARE curr_data DATE;
    DECLARE curr_temp FLOAT;    
    /*current best*/
    DECLARE max_temp FLOAT;
    DECLARE min_temp FLOAT;    
    DECLARE max_created DATE;
    DECLARE min_created DATE;        
     /* absolute best*/
    DECLARE best_max_created DATE;
    DECLARE best_min_created DATE;
    DECLARE best_max_temp FLOAT;
    DECLARE best_min_temp FLOAT;

    DECLARE temptemp FLOAT;
    DECLARE tempcreated DATE;

    DECLARE done INTEGER DEFAULT 0;
    DECLARE cursore_temperature CURSOR FOR
      SELECT
        created,
        ACTUAL_TEMPERATURE
      FROM temperatures
      WHERE created > startingDate
      ORDER BY created;

    DECLARE CONTINUE HANDLER
    FOR NOT FOUND SET done = 1;

       SELECT
        created,
        ACTUAL_TEMPERATURE
        INTO
        min_created,
        min_temp
      FROM temperatures
      ORDER BY created
        LIMIT 1;          

        SELECT
        created,
        ACTUAL_TEMPERATURE
        INTO
        max_created,
        max_temp
      FROM temperatures
      WHERE created > min_created
      ORDER BY created
        LIMIT 1;

        IF min_temp > max_temp
        THEN          
          set temptemp = min_temp;
          set tempcreated =max_created;
          set min_temp = max_temp;
          set min_created = max_created;
          set max_temp = temptemp;
          set max_created = tempcreated;
        END IF;

        set best_max_temp = max_temp;
        set best_min_temp = min_temp;
        set best_max_created = max_created;
        set best_min_created = min_created;

     OPEN cursore_temperature;

    read_loop: LOOP

      FETCH cursore_temperature
      INTO curr_data, curr_temp;

      IF done = 1 
      THEN
        LEAVE read_loop;
      END IF;           

        IF curr_temp < min_temp
        THEN                
            IF (max_temp - min_temp) > ( best_max_temp - best_min_temp)
            THEN
              set best_max_temp = max_temp;
              set best_max_created = max_created;
              set best_min_temp = min_temp;
              set best_min_created = min_created;
            END IF;

          set min_temp = curr_temp;
          set min_created = curr_data;
          set max_temp = curr_temp;     
          set max_created = curr_data;                    
        END IF;

        IF curr_temp > max_temp
        THEN
          set max_temp = curr_temp;
          set max_created = curr_data;
        END IF;

    END LOOP;

    CLOSE cursore_temperature;
    RETURN CONCAT_WS(' | ', best_min_created, best_min_temp, best_max_created, best_max_temp);
  END$$
DELIMITER ;

你称之为

select find_progressive('2017-01-03');

结果是2017-01-03 | 10.1 | 2017-02-05 | 20.8

无论如何,我想这不是最好的谈论优化。

© www.soinside.com 2019 - 2024. All rights reserved.