Postgres: Count number of users actions within time interval












3














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.










share|improve this question







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
















3














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.










share|improve this question







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














3












3








3







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.










share|improve this question







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






share|improve this question







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.











share|improve this question







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.









share|improve this question




share|improve this question






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


















  • 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












2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer





















  • Thanks! It works :)
    – Anna Morozova
    Dec 27 at 14:06



















1














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






share|improve this answer





















  • This looks at whole weeks, not the last 7 days?
    – Andomar
    Dec 27 at 13:57











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.










draft saved

draft discarded


















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









1














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.






share|improve this answer





















  • Thanks! It works :)
    – Anna Morozova
    Dec 27 at 14:06
















1














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.






share|improve this answer





















  • Thanks! It works :)
    – Anna Morozova
    Dec 27 at 14:06














1












1








1






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 27 at 13:47









Andomar

189k33288334




189k33288334












  • 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




Thanks! It works :)
– Anna Morozova
Dec 27 at 14:06













1














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






share|improve this answer





















  • This looks at whole weeks, not the last 7 days?
    – Andomar
    Dec 27 at 13:57
















1














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






share|improve this answer





















  • This looks at whole weeks, not the last 7 days?
    – Andomar
    Dec 27 at 13:57














1












1








1






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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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










Anna Morozova is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'