Time addition based on previous column partition by different object
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?
mysql mysql-5.7
New contributor
add a comment |
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?
mysql mysql-5.7
New contributor
float(15)
? Please read aboutTINYINT UNSIGNED
.
– Rick James
22 hours ago
@Rick James Thank you. I will pay attention to this.
– Deepleeqe
21 hours ago
add a comment |
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?
mysql mysql-5.7
New contributor
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?
mysql mysql-5.7
mysql mysql-5.7
New contributor
New contributor
edited 23 hours ago
Deepleeqe
New contributor
asked yesterday
DeepleeqeDeepleeqe
84
84
New contributor
New contributor
float(15)
? Please read aboutTINYINT UNSIGNED
.
– Rick James
22 hours ago
@Rick James Thank you. I will pay attention to this.
– Deepleeqe
21 hours ago
add a comment |
float(15)
? Please read aboutTINYINT UNSIGNED
.
– Rick James
22 hours ago
@Rick James Thank you. I will pay attention to this.
– Deepleeqe
21 hours ago
float(15)
? Please read about TINYINT UNSIGNED
.– Rick James
22 hours ago
float(15)
? Please read about TINYINT UNSIGNED
.– Rick James
22 hours ago
@Rick James Thank you. I will pay attention to this.
– Deepleeqe
21 hours ago
@Rick James Thank you. I will pay attention to this.
– Deepleeqe
21 hours ago
add a comment |
1 Answer
1
active
oldest
votes
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
It works. Thanks a lot. Much appreciated.
– Deepleeqe
21 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Deepleeqe is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227447%2ftime-addition-based-on-previous-column-partition-by-different-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
It works. Thanks a lot. Much appreciated.
– Deepleeqe
21 hours ago
add a comment |
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
It works. Thanks a lot. Much appreciated.
– Deepleeqe
21 hours ago
add a comment |
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
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
answered 22 hours ago
AkinaAkina
3,6181311
3,6181311
It works. Thanks a lot. Much appreciated.
– Deepleeqe
21 hours ago
add a comment |
It works. Thanks a lot. Much appreciated.
– Deepleeqe
21 hours ago
It works. Thanks a lot. Much appreciated.
– Deepleeqe
21 hours ago
It works. Thanks a lot. Much appreciated.
– Deepleeqe
21 hours ago
add a comment |
Deepleeqe is a new contributor. Be nice, and check out our Code of Conduct.
Deepleeqe is a new contributor. Be nice, and check out our Code of Conduct.
Deepleeqe is a new contributor. Be nice, and check out our Code of Conduct.
Deepleeqe is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f227447%2ftime-addition-based-on-previous-column-partition-by-different-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
float(15)
? Please read aboutTINYINT UNSIGNED
.– Rick James
22 hours ago
@Rick James Thank you. I will pay attention to this.
– Deepleeqe
21 hours ago