How to use a shifting function in postgres to determine time difference by row?
I am looking to use some sort of shift function in postgresql to figure out time difference by row. Below i have a table of workers with start and stop times for each of two companies. I can do this very easily in R. Here is my data set print out and the dput for it :
print:
id start_time stop_time company time_diff
1: worker_1 2017-10-20 22:06:23 2017-10-20 22:09:48 company 1 NA
2: worker_1 2017-10-20 22:28:45 2017-10-20 23:16:56 company 2 0.31583333
3: worker_1 2017-10-20 22:37:07 2017-10-20 22:43:14 company 1 -0.66361111
4: worker_1 2017-10-20 22:59:30 2017-10-20 23:05:52 company 1 0.27111111
5: worker_1 2017-10-20 23:39:57 2017-10-20 23:43:00 company 1 0.56805556
6: worker_1 2017-10-20 23:53:54 2017-10-21 00:07:27 company 1 0.18166667
7: worker_1 2017-10-20 23:53:54 2017-10-21 00:07:27 company 1 -0.22583333
8: worker_2 2017-10-19 18:03:43 2017-10-19 18:04:48 company 1 NA
9: worker_2 2017-10-19 18:04:48 2017-10-19 18:05:08 company 1 0.00000000
10: worker_2 2017-10-19 18:05:51 2017-10-19 18:18:47 company 1 0.01194444
11: worker_2 2017-10-19 18:50:39 2017-10-19 21:01:14 company 1 0.53111111
dput:
structure(list(id = c("worker_1", "worker_1", "worker_1", "worker_1",
"worker_1", "worker_1", "worker_1", "worker_2", "worker_2", "worker_2",
"worker_2"), start_time = structure(c(1508551583, 1508552925,
1508553427, 1508554770, 1508557197, 1508558034, 1508558034, 1508450623,
1508450688, 1508450751, 1508453439), class = c("POSIXct", "POSIXt"
)), stop_time = structure(c(1508551788, 1508555816, 1508553794,
1508555152, 1508557380, 1508558847, 1508558847, 1508450688, 1508450708,
1508451527, 1508461274), class = c("POSIXct", "POSIXt")), company = c("company 1",
"company 2", "company 1", "company 1", "company 1", "company 1",
"company 1", "company 1", "company 1", "company 1", "company 1"
), time_diff = c(NA, 0.315833333333333, -0.663611111111111, 0.271111111111111,
0.568055555555556, 0.181666666666667, -0.225833333333333, NA,
0, 0.0119444444444444, 0.531111111111111)), row.names = c(NA,
-11L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000007a81ef0>)
I create the time_diff column using the following code with data.table
package:
my_data[,time_diff:=as.numeric(
difftime(start_time
,shift(stop_time, type = "lag")
, units = "hours")
),.(id)]
How do i replicate this in postgresql?
r postgresql datetime difftime
add a comment |
I am looking to use some sort of shift function in postgresql to figure out time difference by row. Below i have a table of workers with start and stop times for each of two companies. I can do this very easily in R. Here is my data set print out and the dput for it :
print:
id start_time stop_time company time_diff
1: worker_1 2017-10-20 22:06:23 2017-10-20 22:09:48 company 1 NA
2: worker_1 2017-10-20 22:28:45 2017-10-20 23:16:56 company 2 0.31583333
3: worker_1 2017-10-20 22:37:07 2017-10-20 22:43:14 company 1 -0.66361111
4: worker_1 2017-10-20 22:59:30 2017-10-20 23:05:52 company 1 0.27111111
5: worker_1 2017-10-20 23:39:57 2017-10-20 23:43:00 company 1 0.56805556
6: worker_1 2017-10-20 23:53:54 2017-10-21 00:07:27 company 1 0.18166667
7: worker_1 2017-10-20 23:53:54 2017-10-21 00:07:27 company 1 -0.22583333
8: worker_2 2017-10-19 18:03:43 2017-10-19 18:04:48 company 1 NA
9: worker_2 2017-10-19 18:04:48 2017-10-19 18:05:08 company 1 0.00000000
10: worker_2 2017-10-19 18:05:51 2017-10-19 18:18:47 company 1 0.01194444
11: worker_2 2017-10-19 18:50:39 2017-10-19 21:01:14 company 1 0.53111111
dput:
structure(list(id = c("worker_1", "worker_1", "worker_1", "worker_1",
"worker_1", "worker_1", "worker_1", "worker_2", "worker_2", "worker_2",
"worker_2"), start_time = structure(c(1508551583, 1508552925,
1508553427, 1508554770, 1508557197, 1508558034, 1508558034, 1508450623,
1508450688, 1508450751, 1508453439), class = c("POSIXct", "POSIXt"
)), stop_time = structure(c(1508551788, 1508555816, 1508553794,
1508555152, 1508557380, 1508558847, 1508558847, 1508450688, 1508450708,
1508451527, 1508461274), class = c("POSIXct", "POSIXt")), company = c("company 1",
"company 2", "company 1", "company 1", "company 1", "company 1",
"company 1", "company 1", "company 1", "company 1", "company 1"
), time_diff = c(NA, 0.315833333333333, -0.663611111111111, 0.271111111111111,
0.568055555555556, 0.181666666666667, -0.225833333333333, NA,
0, 0.0119444444444444, 0.531111111111111)), row.names = c(NA,
-11L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000007a81ef0>)
I create the time_diff column using the following code with data.table
package:
my_data[,time_diff:=as.numeric(
difftime(start_time
,shift(stop_time, type = "lag")
, units = "hours")
),.(id)]
How do i replicate this in postgresql?
r postgresql datetime difftime
Related: stackoverflow.com/questions/17301816/lag-function-and-group-by
– IceCreamToucan
Nov 20 '18 at 20:58
looks likeselect start_time - lag(stop_time) over(partition by id) from df
– IceCreamToucan
Nov 20 '18 at 21:15
this was perfect, thank you
– LoF10
Nov 20 '18 at 21:43
add a comment |
I am looking to use some sort of shift function in postgresql to figure out time difference by row. Below i have a table of workers with start and stop times for each of two companies. I can do this very easily in R. Here is my data set print out and the dput for it :
print:
id start_time stop_time company time_diff
1: worker_1 2017-10-20 22:06:23 2017-10-20 22:09:48 company 1 NA
2: worker_1 2017-10-20 22:28:45 2017-10-20 23:16:56 company 2 0.31583333
3: worker_1 2017-10-20 22:37:07 2017-10-20 22:43:14 company 1 -0.66361111
4: worker_1 2017-10-20 22:59:30 2017-10-20 23:05:52 company 1 0.27111111
5: worker_1 2017-10-20 23:39:57 2017-10-20 23:43:00 company 1 0.56805556
6: worker_1 2017-10-20 23:53:54 2017-10-21 00:07:27 company 1 0.18166667
7: worker_1 2017-10-20 23:53:54 2017-10-21 00:07:27 company 1 -0.22583333
8: worker_2 2017-10-19 18:03:43 2017-10-19 18:04:48 company 1 NA
9: worker_2 2017-10-19 18:04:48 2017-10-19 18:05:08 company 1 0.00000000
10: worker_2 2017-10-19 18:05:51 2017-10-19 18:18:47 company 1 0.01194444
11: worker_2 2017-10-19 18:50:39 2017-10-19 21:01:14 company 1 0.53111111
dput:
structure(list(id = c("worker_1", "worker_1", "worker_1", "worker_1",
"worker_1", "worker_1", "worker_1", "worker_2", "worker_2", "worker_2",
"worker_2"), start_time = structure(c(1508551583, 1508552925,
1508553427, 1508554770, 1508557197, 1508558034, 1508558034, 1508450623,
1508450688, 1508450751, 1508453439), class = c("POSIXct", "POSIXt"
)), stop_time = structure(c(1508551788, 1508555816, 1508553794,
1508555152, 1508557380, 1508558847, 1508558847, 1508450688, 1508450708,
1508451527, 1508461274), class = c("POSIXct", "POSIXt")), company = c("company 1",
"company 2", "company 1", "company 1", "company 1", "company 1",
"company 1", "company 1", "company 1", "company 1", "company 1"
), time_diff = c(NA, 0.315833333333333, -0.663611111111111, 0.271111111111111,
0.568055555555556, 0.181666666666667, -0.225833333333333, NA,
0, 0.0119444444444444, 0.531111111111111)), row.names = c(NA,
-11L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000007a81ef0>)
I create the time_diff column using the following code with data.table
package:
my_data[,time_diff:=as.numeric(
difftime(start_time
,shift(stop_time, type = "lag")
, units = "hours")
),.(id)]
How do i replicate this in postgresql?
r postgresql datetime difftime
I am looking to use some sort of shift function in postgresql to figure out time difference by row. Below i have a table of workers with start and stop times for each of two companies. I can do this very easily in R. Here is my data set print out and the dput for it :
print:
id start_time stop_time company time_diff
1: worker_1 2017-10-20 22:06:23 2017-10-20 22:09:48 company 1 NA
2: worker_1 2017-10-20 22:28:45 2017-10-20 23:16:56 company 2 0.31583333
3: worker_1 2017-10-20 22:37:07 2017-10-20 22:43:14 company 1 -0.66361111
4: worker_1 2017-10-20 22:59:30 2017-10-20 23:05:52 company 1 0.27111111
5: worker_1 2017-10-20 23:39:57 2017-10-20 23:43:00 company 1 0.56805556
6: worker_1 2017-10-20 23:53:54 2017-10-21 00:07:27 company 1 0.18166667
7: worker_1 2017-10-20 23:53:54 2017-10-21 00:07:27 company 1 -0.22583333
8: worker_2 2017-10-19 18:03:43 2017-10-19 18:04:48 company 1 NA
9: worker_2 2017-10-19 18:04:48 2017-10-19 18:05:08 company 1 0.00000000
10: worker_2 2017-10-19 18:05:51 2017-10-19 18:18:47 company 1 0.01194444
11: worker_2 2017-10-19 18:50:39 2017-10-19 21:01:14 company 1 0.53111111
dput:
structure(list(id = c("worker_1", "worker_1", "worker_1", "worker_1",
"worker_1", "worker_1", "worker_1", "worker_2", "worker_2", "worker_2",
"worker_2"), start_time = structure(c(1508551583, 1508552925,
1508553427, 1508554770, 1508557197, 1508558034, 1508558034, 1508450623,
1508450688, 1508450751, 1508453439), class = c("POSIXct", "POSIXt"
)), stop_time = structure(c(1508551788, 1508555816, 1508553794,
1508555152, 1508557380, 1508558847, 1508558847, 1508450688, 1508450708,
1508451527, 1508461274), class = c("POSIXct", "POSIXt")), company = c("company 1",
"company 2", "company 1", "company 1", "company 1", "company 1",
"company 1", "company 1", "company 1", "company 1", "company 1"
), time_diff = c(NA, 0.315833333333333, -0.663611111111111, 0.271111111111111,
0.568055555555556, 0.181666666666667, -0.225833333333333, NA,
0, 0.0119444444444444, 0.531111111111111)), row.names = c(NA,
-11L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000007a81ef0>)
I create the time_diff column using the following code with data.table
package:
my_data[,time_diff:=as.numeric(
difftime(start_time
,shift(stop_time, type = "lag")
, units = "hours")
),.(id)]
How do i replicate this in postgresql?
r postgresql datetime difftime
r postgresql datetime difftime
asked Nov 20 '18 at 20:57
LoF10LoF10
154214
154214
Related: stackoverflow.com/questions/17301816/lag-function-and-group-by
– IceCreamToucan
Nov 20 '18 at 20:58
looks likeselect start_time - lag(stop_time) over(partition by id) from df
– IceCreamToucan
Nov 20 '18 at 21:15
this was perfect, thank you
– LoF10
Nov 20 '18 at 21:43
add a comment |
Related: stackoverflow.com/questions/17301816/lag-function-and-group-by
– IceCreamToucan
Nov 20 '18 at 20:58
looks likeselect start_time - lag(stop_time) over(partition by id) from df
– IceCreamToucan
Nov 20 '18 at 21:15
this was perfect, thank you
– LoF10
Nov 20 '18 at 21:43
Related: stackoverflow.com/questions/17301816/lag-function-and-group-by
– IceCreamToucan
Nov 20 '18 at 20:58
Related: stackoverflow.com/questions/17301816/lag-function-and-group-by
– IceCreamToucan
Nov 20 '18 at 20:58
looks like
select start_time - lag(stop_time) over(partition by id) from df
– IceCreamToucan
Nov 20 '18 at 21:15
looks like
select start_time - lag(stop_time) over(partition by id) from df
– IceCreamToucan
Nov 20 '18 at 21:15
this was perfect, thank you
– LoF10
Nov 20 '18 at 21:43
this was perfect, thank you
– LoF10
Nov 20 '18 at 21:43
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53401410%2fhow-to-use-a-shifting-function-in-postgres-to-determine-time-difference-by-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53401410%2fhow-to-use-a-shifting-function-in-postgres-to-determine-time-difference-by-row%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
Related: stackoverflow.com/questions/17301816/lag-function-and-group-by
– IceCreamToucan
Nov 20 '18 at 20:58
looks like
select start_time - lag(stop_time) over(partition by id) from df
– IceCreamToucan
Nov 20 '18 at 21:15
this was perfect, thank you
– LoF10
Nov 20 '18 at 21:43