Postgres: Count number of users actions within time interval
I am trying to count kind of DAU by specified actions, where users are divided into categories by number of their actions within some interval.
Raw data example:
date user_id amount_actions
2018-12-01 1 2
2018-12-02 1 1
2018-12-10 1 1
2018-12-01 2 2
2018-12-02 2 1
2018-12-10 3 1
Result table I wish I could have:
date user_id amount_actions rolling_sum_7_days
2018-12-01 1 2 2
2018-12-02 1 1 3
2018-12-10 1 1 1
2018-12-01 2 2 2
2018-12-12 2 1 1
2018-12-10 3 1 1
2018-12-15 3 1 2
Thanks.
postgresql
New contributor
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am trying to count kind of DAU by specified actions, where users are divided into categories by number of their actions within some interval.
Raw data example:
date user_id amount_actions
2018-12-01 1 2
2018-12-02 1 1
2018-12-10 1 1
2018-12-01 2 2
2018-12-02 2 1
2018-12-10 3 1
Result table I wish I could have:
date user_id amount_actions rolling_sum_7_days
2018-12-01 1 2 2
2018-12-02 1 1 3
2018-12-10 1 1 1
2018-12-01 2 2 2
2018-12-12 2 1 1
2018-12-10 3 1 1
2018-12-15 3 1 2
Thanks.
postgresql
New contributor
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Why source table has 6 rows and result has 7 rows?
– McNets
Dec 27 at 13:48
Sorry, did a mistake while copying
– Anna Morozova
Dec 27 at 14:07
add a comment |
I am trying to count kind of DAU by specified actions, where users are divided into categories by number of their actions within some interval.
Raw data example:
date user_id amount_actions
2018-12-01 1 2
2018-12-02 1 1
2018-12-10 1 1
2018-12-01 2 2
2018-12-02 2 1
2018-12-10 3 1
Result table I wish I could have:
date user_id amount_actions rolling_sum_7_days
2018-12-01 1 2 2
2018-12-02 1 1 3
2018-12-10 1 1 1
2018-12-01 2 2 2
2018-12-12 2 1 1
2018-12-10 3 1 1
2018-12-15 3 1 2
Thanks.
postgresql
New contributor
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am trying to count kind of DAU by specified actions, where users are divided into categories by number of their actions within some interval.
Raw data example:
date user_id amount_actions
2018-12-01 1 2
2018-12-02 1 1
2018-12-10 1 1
2018-12-01 2 2
2018-12-02 2 1
2018-12-10 3 1
Result table I wish I could have:
date user_id amount_actions rolling_sum_7_days
2018-12-01 1 2 2
2018-12-02 1 1 3
2018-12-10 1 1 1
2018-12-01 2 2 2
2018-12-12 2 1 1
2018-12-10 3 1 1
2018-12-15 3 1 2
Thanks.
postgresql
postgresql
New contributor
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Dec 27 at 13:31
Anna Morozova
183
183
New contributor
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Anna Morozova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Why source table has 6 rows and result has 7 rows?
– McNets
Dec 27 at 13:48
Sorry, did a mistake while copying
– Anna Morozova
Dec 27 at 14:07
add a comment |
Why source table has 6 rows and result has 7 rows?
– McNets
Dec 27 at 13:48
Sorry, did a mistake while copying
– Anna Morozova
Dec 27 at 14:07
Why source table has 6 rows and result has 7 rows?
– McNets
Dec 27 at 13:48
Why source table has 6 rows and result has 7 rows?
– McNets
Dec 27 at 13:48
Sorry, did a mistake while copying
– Anna Morozova
Dec 27 at 14:07
Sorry, did a mistake while copying
– Anna Morozova
Dec 27 at 14:07
add a comment |
2 Answers
2
active
oldest
votes
You can do a lateral join that calculates the sum of actions for that user in the past seven days:
select date
, user_id
, amount_actions
, sum_actions
from YourTable yt1
cross join lateral
(
select sum(amount_actions) as sum_actions
from YourTable yt2
where yt1.user_id = yt2.user_id
and yt1.date - interval '7 days' < yt2.date
and yt2.date <= yt1.date
) sum_actions
Working example at rextester.
Thanks! It works :)
– Anna Morozova
Dec 27 at 14:06
add a comment |
Using a cumulative sum on Postgres:
select
dt, user_id, amount_actions,
to_char(dt, 'WWYYYY') wk,
sum(amount_actions)
over
(partition by user_id, to_char(dt, 'WWYYYY')
order by user_id, dt) rolling_sum_7_days
from
tbl
order by user_id, dt;
The partition is: user_id + WeekYear to_char(dt, 'WWYYYY')
dt | user_id | amount_actions | wk | rolling_sum_7_days
:--------- | ------: | -------------: | :----- | -----------------:
2018-12-01 | 1 | 2 | 482018 | 2
2018-12-02 | 1 | 1 | 482018 | 3
2018-12-10 | 1 | 1 | 502018 | 1
2018-12-01 | 2 | 2 | 482018 | 2
2018-12-02 | 2 | 1 | 482018 | 3
2018-12-10 | 3 | 1 | 502018 | 1
db<>fiddle here
This looks at whole weeks, not the last 7 days?
– Andomar
Dec 27 at 13:57
add a comment |
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
});
}
});
Anna Morozova 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%2fstackoverflow.com%2fquestions%2f53945936%2fpostgres-count-number-of-users-actions-within-time-interval%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do a lateral join that calculates the sum of actions for that user in the past seven days:
select date
, user_id
, amount_actions
, sum_actions
from YourTable yt1
cross join lateral
(
select sum(amount_actions) as sum_actions
from YourTable yt2
where yt1.user_id = yt2.user_id
and yt1.date - interval '7 days' < yt2.date
and yt2.date <= yt1.date
) sum_actions
Working example at rextester.
Thanks! It works :)
– Anna Morozova
Dec 27 at 14:06
add a comment |
You can do a lateral join that calculates the sum of actions for that user in the past seven days:
select date
, user_id
, amount_actions
, sum_actions
from YourTable yt1
cross join lateral
(
select sum(amount_actions) as sum_actions
from YourTable yt2
where yt1.user_id = yt2.user_id
and yt1.date - interval '7 days' < yt2.date
and yt2.date <= yt1.date
) sum_actions
Working example at rextester.
Thanks! It works :)
– Anna Morozova
Dec 27 at 14:06
add a comment |
You can do a lateral join that calculates the sum of actions for that user in the past seven days:
select date
, user_id
, amount_actions
, sum_actions
from YourTable yt1
cross join lateral
(
select sum(amount_actions) as sum_actions
from YourTable yt2
where yt1.user_id = yt2.user_id
and yt1.date - interval '7 days' < yt2.date
and yt2.date <= yt1.date
) sum_actions
Working example at rextester.
You can do a lateral join that calculates the sum of actions for that user in the past seven days:
select date
, user_id
, amount_actions
, sum_actions
from YourTable yt1
cross join lateral
(
select sum(amount_actions) as sum_actions
from YourTable yt2
where yt1.user_id = yt2.user_id
and yt1.date - interval '7 days' < yt2.date
and yt2.date <= yt1.date
) sum_actions
Working example at rextester.
answered Dec 27 at 13:47
Andomar
189k33288334
189k33288334
Thanks! It works :)
– Anna Morozova
Dec 27 at 14:06
add a comment |
Thanks! It works :)
– Anna Morozova
Dec 27 at 14:06
Thanks! It works :)
– Anna Morozova
Dec 27 at 14:06
Thanks! It works :)
– Anna Morozova
Dec 27 at 14:06
add a comment |
Using a cumulative sum on Postgres:
select
dt, user_id, amount_actions,
to_char(dt, 'WWYYYY') wk,
sum(amount_actions)
over
(partition by user_id, to_char(dt, 'WWYYYY')
order by user_id, dt) rolling_sum_7_days
from
tbl
order by user_id, dt;
The partition is: user_id + WeekYear to_char(dt, 'WWYYYY')
dt | user_id | amount_actions | wk | rolling_sum_7_days
:--------- | ------: | -------------: | :----- | -----------------:
2018-12-01 | 1 | 2 | 482018 | 2
2018-12-02 | 1 | 1 | 482018 | 3
2018-12-10 | 1 | 1 | 502018 | 1
2018-12-01 | 2 | 2 | 482018 | 2
2018-12-02 | 2 | 1 | 482018 | 3
2018-12-10 | 3 | 1 | 502018 | 1
db<>fiddle here
This looks at whole weeks, not the last 7 days?
– Andomar
Dec 27 at 13:57
add a comment |
Using a cumulative sum on Postgres:
select
dt, user_id, amount_actions,
to_char(dt, 'WWYYYY') wk,
sum(amount_actions)
over
(partition by user_id, to_char(dt, 'WWYYYY')
order by user_id, dt) rolling_sum_7_days
from
tbl
order by user_id, dt;
The partition is: user_id + WeekYear to_char(dt, 'WWYYYY')
dt | user_id | amount_actions | wk | rolling_sum_7_days
:--------- | ------: | -------------: | :----- | -----------------:
2018-12-01 | 1 | 2 | 482018 | 2
2018-12-02 | 1 | 1 | 482018 | 3
2018-12-10 | 1 | 1 | 502018 | 1
2018-12-01 | 2 | 2 | 482018 | 2
2018-12-02 | 2 | 1 | 482018 | 3
2018-12-10 | 3 | 1 | 502018 | 1
db<>fiddle here
This looks at whole weeks, not the last 7 days?
– Andomar
Dec 27 at 13:57
add a comment |
Using a cumulative sum on Postgres:
select
dt, user_id, amount_actions,
to_char(dt, 'WWYYYY') wk,
sum(amount_actions)
over
(partition by user_id, to_char(dt, 'WWYYYY')
order by user_id, dt) rolling_sum_7_days
from
tbl
order by user_id, dt;
The partition is: user_id + WeekYear to_char(dt, 'WWYYYY')
dt | user_id | amount_actions | wk | rolling_sum_7_days
:--------- | ------: | -------------: | :----- | -----------------:
2018-12-01 | 1 | 2 | 482018 | 2
2018-12-02 | 1 | 1 | 482018 | 3
2018-12-10 | 1 | 1 | 502018 | 1
2018-12-01 | 2 | 2 | 482018 | 2
2018-12-02 | 2 | 1 | 482018 | 3
2018-12-10 | 3 | 1 | 502018 | 1
db<>fiddle here
Using a cumulative sum on Postgres:
select
dt, user_id, amount_actions,
to_char(dt, 'WWYYYY') wk,
sum(amount_actions)
over
(partition by user_id, to_char(dt, 'WWYYYY')
order by user_id, dt) rolling_sum_7_days
from
tbl
order by user_id, dt;
The partition is: user_id + WeekYear to_char(dt, 'WWYYYY')
dt | user_id | amount_actions | wk | rolling_sum_7_days
:--------- | ------: | -------------: | :----- | -----------------:
2018-12-01 | 1 | 2 | 482018 | 2
2018-12-02 | 1 | 1 | 482018 | 3
2018-12-10 | 1 | 1 | 502018 | 1
2018-12-01 | 2 | 2 | 482018 | 2
2018-12-02 | 2 | 1 | 482018 | 3
2018-12-10 | 3 | 1 | 502018 | 1
db<>fiddle here
answered Dec 27 at 13:50
McNets
7,77821733
7,77821733
This looks at whole weeks, not the last 7 days?
– Andomar
Dec 27 at 13:57
add a comment |
This looks at whole weeks, not the last 7 days?
– Andomar
Dec 27 at 13:57
This looks at whole weeks, not the last 7 days?
– Andomar
Dec 27 at 13:57
This looks at whole weeks, not the last 7 days?
– Andomar
Dec 27 at 13:57
add a comment |
Anna Morozova is a new contributor. Be nice, and check out our Code of Conduct.
Anna Morozova is a new contributor. Be nice, and check out our Code of Conduct.
Anna Morozova is a new contributor. Be nice, and check out our Code of Conduct.
Anna Morozova is a new contributor. Be nice, and check out our Code of Conduct.
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53945936%2fpostgres-count-number-of-users-actions-within-time-interval%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
Why source table has 6 rows and result has 7 rows?
– McNets
Dec 27 at 13:48
Sorry, did a mistake while copying
– Anna Morozova
Dec 27 at 14:07