0

I asked a question on Time addition based on the previous row.

Now I have another one related to that question. Appreciate if someone could give me some help. I use the following code to create the data my need.

create temporary table test(object varchar(10),
                            id integer,
                            realtime datetime,
                            realminute float(15));

insert into test(object, id, realtime, realminute) values
("A","1","2018-12-12 03:03:03","96"),
("B","1","2018-12-12 05:24:01","15");

insert into test(object, id, realminute) values
("A","2","15"),
("A","3","10"),
("A","4","30"),
("A","5","30"),
("A","6","15"),
("B","2","15"),
("B","3","15"),
("B","4","15"),
("B","5","15"),
("B","6","15");

Then I have this.

object|id|          realtime                      | realminute
     A|1|           2018-12-12 03:03:03           |  96
     A|2|                                         |  15
     A|3|                                         |  10
     A|4|                                         |  30
     A|5|                                         |  30
     A|6|                                         |  15
     B|1|           2018-12-12 05:24:01           |  15
     B|2|                                         |  15
     B|3|                                         |  15
     B|4|                                         |  15
     B|5|                                         |  15
     B|6|                                         |  15

For object A, 2018-12-12 03:03:03 plus 96 minutes would be 2018-12-12 04:39:03. This is the realtime for object A in the second column. For object B, 2018-12-12 05:24:01 plus 15 minutes would be 2018-12-12 05:39:01. This is the realtime for object B in the second column. The following table is what I need.

object|id|          realtime  | realminute
     A|1| 2018-12-12 03:03:03 |  96
     A|2| 2018-12-12 04:39:03 |  15
     A|3| 2018-12-12 04:54:03 |  10
     A|4| 2018-12-12 05:04:03 |  30
     A|5| 2018-12-12 05:34:03 |  30
     A|6| 2018-12-12 06:04:03 |  15
     B|1| 2018-12-12 05:24:01 |  15
     B|2| 2018-12-12 05:39:01 |  15
     B|3| 2018-12-12 05:54:01 |  15
     B|4| 2018-12-12 06:09:01 |  15
     B|5| 2018-12-12 06:24:01 |  15
     B|6| 2018-12-12 06:39:01 |  15

How could I get this result?

2
  • float(15)? Please read about TINYINT UNSIGNED.
    – Rick James
    Commented Jan 18, 2019 at 5:20
  • @Rick James Thank you. I will pay attention to this.
    – Deepleeqe
    Commented Jan 18, 2019 at 5:53

1 Answer 1

0
SELECT CASE WHEN realtime IS NULL AND @object = object 
            THEN @realtime 
            ELSE @realtime := realtime 
            END realtime,
       realminute,
       @object := object object,
       id,
       @realtime := @realtime + INTERVAL realminute MINUTE nextrealtime
FROM test, (SELECT @object:='',@realtime:='') vars
ORDER BY object, id;

-

| realtime            | object | nextrealtime        | realminute | id  |
| ------------------- | ------ | ------------------- | ---------- | --- |
| 2018-12-12 03:03:03 | A      | 2018-12-12 04:39:03 | 96         | 1   |
| 2018-12-12 04:39:03 | A      | 2018-12-12 04:54:03 | 15         | 2   |
| 2018-12-12 04:54:03 | A      | 2018-12-12 05:04:03 | 10         | 3   |
| 2018-12-12 05:04:03 | A      | 2018-12-12 05:34:03 | 30         | 4   |
| 2018-12-12 05:34:03 | A      | 2018-12-12 06:04:03 | 30         | 5   |
| 2018-12-12 06:04:03 | A      | 2018-12-12 06:19:03 | 15         | 6   |
| 2018-12-12 05:24:01 | B      | 2018-12-12 05:39:01 | 15         | 1   |
| 2018-12-12 05:39:01 | B      | 2018-12-12 05:54:01 | 15         | 2   |
| 2018-12-12 05:54:01 | B      | 2018-12-12 06:09:01 | 15         | 3   |
| 2018-12-12 06:09:01 | B      | 2018-12-12 06:24:01 | 15         | 4   |
| 2018-12-12 06:24:01 | B      | 2018-12-12 06:39:01 | 15         | 5   |
| 2018-12-12 06:39:01 | B      | 2018-12-12 06:54:01 | 15         | 6   |

View on DB Fiddle

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.