2017-01-17 41 views
2

我有下表。在2.5分鐘(150秒)的情況下,兩個工廠有一個記錄。我想得到一個結果,顯示各工廠的最大值(日期時間)和最小值(日期時間)。但是,對於每個國家的變化,都應該有新的路線。組狀態相同,直到狀態改變

plant state unix_timest datetime 
    1 1  1452639750 2016-01-13 00:02:30 
    2 1  1452639750 2016-01-13 00:02:30 
    1 1  1452639900 2016-01-13 00:05:00 
    2 1  1452639900 2016-01-13 00:05:00 
    1 1  1452640050 2016-01-13 00:07:30 
    2 1  1452640050 2016-01-13 00:07:30 
    1 1  1452640200 2016-01-13 00:10:00 
    2 1  1452640200 2016-01-13 00:10:00 
    1 1  1452640350 2016-01-13 00:12:30 
    2 0  1452640350 2016-01-13 00:12:30 
    1 1  1452640500 2016-01-13 00:15:00 
    2 0  1452640500 2016-01-13 00:15:00 
    1 1  1452640650 2016-01-13 00:17:30 
    2 1  1452640650 2016-01-13 00:17:30 
    1 1  1452640800 2016-01-13 00:20:00 
    2 1  1452640800 2016-01-13 00:20:00 

我的SQL語句是:

SELECT a.line, a.current_state, min(a.`datetime`) AS mindate, max(a.`datetime`) AS maxdate 
    FROM `bde_production` 
    LEFT JOIN bde_production a ON a.unix_timestamp = bde_production.unix_timestamp 
    LEFT JOIN bde_production b ON b.unix_timestamp = bde_production.unix_timestamp -150 
    GROUP BY a.line, a.current_state ORDER BY a.line, mindate 

結果:

plant current_state mindate    maxdate 
    1 1    2016-01-13 00:02:30 2016-01-13 00:20:00 
    2 1    2016-01-13 00:02:30 2016-01-13 00:20:00 
    2 0    2016-01-13 00:12:30 2016-01-13 00:15:00 

我希望得到什麼:

plant current_state mindate    maxdate 
    1 1    2016-01-13 00:02:30 2016-01-13 00:20:00 
    2 1    2016-01-13 00:02:30 2016-01-13 00:10:00 
    2 0    2016-01-13 00:12:30 2016-01-13 00:15:00 
    2 1    2016-01-13 00:17:30 2016-01-13 00:20:00 

感謝

回答

-1

我差點與結果:

SELECT 
     d.plant, d.current_state, d.unix_timestamp, d.`datetime` 
    FROM 
     bde_production d 
      left join 
     bde_production d_prev 
      on 
      d_prev.line = d.line and 
      d_prev.unix_timestamp< (d.unix_timestamp) 
      left join 
     bde_production d_inter 
      on 
       d_prev.plant= d.plant and 
       d_inter.unix_timestamp< (d.unix_timestamp) and 
       d_prev.unix_timestamp< (d_inter.unix_timestamp) 

    WHERE 
     d_inter.unix_timestamp is null AND 
     (d_prev.current_state is null OR d_prev.current_state <> d.current_state 

) 

結果:

line current_state unix_timestamp datetime 
1  1    1452639750 2016-01-13 00:02:30 
2  1    1452639750 2016-01-13 00:02:30 
2  0    1452640350 2016-01-13 00:12:30 
2  1    1452640650 2016-01-13 00:17:30 

我只想與下一行,其中current_state改變

0

同樣的datetime增加一列我沒有得到正確的結果,但是如果有幫助,請留在我的手中。我的基本思想是找到所有的變化,爲他們分配一個id(change_num),並按轉換分組。我稍後再編輯

CREATE TABLE bde_production (
    `id`   INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    `plant`  INT(3) UNSIGNED  NOT NULL, 
    `state`  TINYINT(1) UNSIGNED NOT NULL, 
    `unix_timest` INT(10) UNSIGNED NOT NULL, 
    `datetime` DATETIME   NOT NULL 
); 

INSERT INTO bde_production (plant, state, unix_timest, datetime) VALUES 
    (1, 1, 1452639750, "2016-01-13 00:02:30"), 
    (2, 1, 1452639750, "2016-01-13 00:02:30"), 
    (1, 1, 1452639900, "2016-01-13 00:05:00"), 
    (2, 1, 1452639900, "2016-01-13 00:05:00"), 
    (1, 1, 1452640050, "2016-01-13 00:07:30"), 
    (2, 1, 1452640050, "2016-01-13 00:07:30"), 
    (1, 1, 1452640200, "2016-01-13 00:10:00"), 
    (2, 1, 1452640200, "2016-01-13 00:10:00"), 
    (1, 1, 1452640350, "2016-01-13 00:12:30"), 
    (2, 0, 1452640350, "2016-01-13 00:12:30"), 
    (1, 1, 1452640500, "2016-01-13 00:15:00"), 
    (2, 0, 1452640500, "2016-01-13 00:15:00"), 
    (1, 1, 1452640650, "2016-01-13 00:17:30"), 
    (2, 1, 1452640650, "2016-01-13 00:17:30"), 
    (1, 1, 1452640800, "2016-01-13 00:20:00"), 
    (2, 1, 1452640800, "2016-01-13 00:20:00"); 


SET @line_num_a = 0; 
SET @line_num_b = 0; 
SET @change_num = 0; 

SELECT plant, 
    IFNULL(`changes_max_state`, `bde_max_state`) AS `current_state`, 
    `bde_min_date`, 
    IFNULL(`changes_max_date`, `bde_max_date`), 
    `changes_max_date`, 
    `bde_max_date` 
FROM (
     SELECT 
     bde.plant, 
     change_num, 
     MIN(bde.datetime) `bde_min_date`, 
     MAX(bde.datetime) `bde_max_date`, 
     MAX(bde.state) `bde_max_state`, 
     MIN(changes.datetime) `changes_min_date`, 
     MAX(changes.state) `changes_max_state`, 
     MAX(changes.datetime) `changes_max_date`-- MIN(bde.datetime), MAX(bde.datetime) 
     , MIN(changes.datetime_b), 
     MAX(changes.datetime_b) 
     FROM 
     bde_production bde LEFT JOIN (
             SELECT 
              # a.datetime is the max reading for plant in a.state 
              b.*, a.datetime AS datetime_b, @change_num := @change_num + 1 `change_num` 
             FROM 
              (
              SELECT 
               *, 
               @line_num_a := @line_num_a + 1 AS `line` 
              FROM (
                SELECT * 
                FROM bde_production 
                ORDER BY plant DESC 
               ) a1) a 
              JOIN (
               SELECT 
                *, 
                @line_num_b := @line_num_b + 1 AS `line` 
               FROM (
                 SELECT * 
                 FROM bde_production 
                 ORDER BY plant DESC 
                ) b1) b 
              ON 
               a.plant = b.plant AND a.line = b.line - 1 AND a.state <> b.state 
            ) changes 
      ON changes.plant=bde.plant AND bde.id > changes.id 
     GROUP BY change_num, plant 
    ) q 
;