How do you aggregate the deltas between two arbitrary datetime columns?












0














Please look at the columns and types of a table called History:



- id PRIMARY KEY 
- account_id STRING
- total_spent FLOAT
- date_created DATETIME


Sample data for this table:



(id, account_id, total_spent, date_created)
(1, '123-456-7890', 10, 2018-10-01 00:00:00+08)
(2, '123-456-7890', 20, 2018-10-02 00:00:00+08)
(3, '123-456-7890', 30, 2018-10-04 00:00:00+08)




If I have to find out the money spent by account 123-456-7890 between 2018-10-01 00:00:01+08 and 2018-10-02 00:00:01+08:




  1. let A = the latest total_spent before 2018-10-01 00:00:01+08

  2. let B = the latest total_spent before 2018-10-02 00:00:01+08

  3. Subtract B - A = 10.




If I have to find the amount of money spent by account 123-456-7890 between 2018-10-03 00:00:01+08 and 2018-10-04 00:00:01+08:




  1. A will be 20 since that is the latest row before 2018-10-03 00:00:01+08

  2. B will be 30

  3. Thus answer is 10 as well.




Questions:




  1. Given this schema, how do you calculate the total amount of money spent by all accounts between two datetime?

  2. If I am interested to calculate spending for accounts between two arbitrary datetime, how can I restructure my schema to make my life easier?










share|improve this question






















  • Please see How to Ask & hits googling 'stackexhange homework'--show what parts you can do & some understanding of relevant aggregation basics & what that suggests here. Parts that you list are faqs, google for them first.
    – philipxy
    Nov 13 '18 at 0:33
















0














Please look at the columns and types of a table called History:



- id PRIMARY KEY 
- account_id STRING
- total_spent FLOAT
- date_created DATETIME


Sample data for this table:



(id, account_id, total_spent, date_created)
(1, '123-456-7890', 10, 2018-10-01 00:00:00+08)
(2, '123-456-7890', 20, 2018-10-02 00:00:00+08)
(3, '123-456-7890', 30, 2018-10-04 00:00:00+08)




If I have to find out the money spent by account 123-456-7890 between 2018-10-01 00:00:01+08 and 2018-10-02 00:00:01+08:




  1. let A = the latest total_spent before 2018-10-01 00:00:01+08

  2. let B = the latest total_spent before 2018-10-02 00:00:01+08

  3. Subtract B - A = 10.




If I have to find the amount of money spent by account 123-456-7890 between 2018-10-03 00:00:01+08 and 2018-10-04 00:00:01+08:




  1. A will be 20 since that is the latest row before 2018-10-03 00:00:01+08

  2. B will be 30

  3. Thus answer is 10 as well.




Questions:




  1. Given this schema, how do you calculate the total amount of money spent by all accounts between two datetime?

  2. If I am interested to calculate spending for accounts between two arbitrary datetime, how can I restructure my schema to make my life easier?










share|improve this question






















  • Please see How to Ask & hits googling 'stackexhange homework'--show what parts you can do & some understanding of relevant aggregation basics & what that suggests here. Parts that you list are faqs, google for them first.
    – philipxy
    Nov 13 '18 at 0:33














0












0








0







Please look at the columns and types of a table called History:



- id PRIMARY KEY 
- account_id STRING
- total_spent FLOAT
- date_created DATETIME


Sample data for this table:



(id, account_id, total_spent, date_created)
(1, '123-456-7890', 10, 2018-10-01 00:00:00+08)
(2, '123-456-7890', 20, 2018-10-02 00:00:00+08)
(3, '123-456-7890', 30, 2018-10-04 00:00:00+08)




If I have to find out the money spent by account 123-456-7890 between 2018-10-01 00:00:01+08 and 2018-10-02 00:00:01+08:




  1. let A = the latest total_spent before 2018-10-01 00:00:01+08

  2. let B = the latest total_spent before 2018-10-02 00:00:01+08

  3. Subtract B - A = 10.




If I have to find the amount of money spent by account 123-456-7890 between 2018-10-03 00:00:01+08 and 2018-10-04 00:00:01+08:




  1. A will be 20 since that is the latest row before 2018-10-03 00:00:01+08

  2. B will be 30

  3. Thus answer is 10 as well.




Questions:




  1. Given this schema, how do you calculate the total amount of money spent by all accounts between two datetime?

  2. If I am interested to calculate spending for accounts between two arbitrary datetime, how can I restructure my schema to make my life easier?










share|improve this question













Please look at the columns and types of a table called History:



- id PRIMARY KEY 
- account_id STRING
- total_spent FLOAT
- date_created DATETIME


Sample data for this table:



(id, account_id, total_spent, date_created)
(1, '123-456-7890', 10, 2018-10-01 00:00:00+08)
(2, '123-456-7890', 20, 2018-10-02 00:00:00+08)
(3, '123-456-7890', 30, 2018-10-04 00:00:00+08)




If I have to find out the money spent by account 123-456-7890 between 2018-10-01 00:00:01+08 and 2018-10-02 00:00:01+08:




  1. let A = the latest total_spent before 2018-10-01 00:00:01+08

  2. let B = the latest total_spent before 2018-10-02 00:00:01+08

  3. Subtract B - A = 10.




If I have to find the amount of money spent by account 123-456-7890 between 2018-10-03 00:00:01+08 and 2018-10-04 00:00:01+08:




  1. A will be 20 since that is the latest row before 2018-10-03 00:00:01+08

  2. B will be 30

  3. Thus answer is 10 as well.




Questions:




  1. Given this schema, how do you calculate the total amount of money spent by all accounts between two datetime?

  2. If I am interested to calculate spending for accounts between two arbitrary datetime, how can I restructure my schema to make my life easier?







database postgresql database-design window-functions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 22:33









Sparrowcide

1,10211332




1,10211332












  • Please see How to Ask & hits googling 'stackexhange homework'--show what parts you can do & some understanding of relevant aggregation basics & what that suggests here. Parts that you list are faqs, google for them first.
    – philipxy
    Nov 13 '18 at 0:33


















  • Please see How to Ask & hits googling 'stackexhange homework'--show what parts you can do & some understanding of relevant aggregation basics & what that suggests here. Parts that you list are faqs, google for them first.
    – philipxy
    Nov 13 '18 at 0:33
















Please see How to Ask & hits googling 'stackexhange homework'--show what parts you can do & some understanding of relevant aggregation basics & what that suggests here. Parts that you list are faqs, google for them first.
– philipxy
Nov 13 '18 at 0:33




Please see How to Ask & hits googling 'stackexhange homework'--show what parts you can do & some understanding of relevant aggregation basics & what that suggests here. Parts that you list are faqs, google for them first.
– philipxy
Nov 13 '18 at 0:33

















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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271060%2fhow-do-you-aggregate-the-deltas-between-two-arbitrary-datetime-columns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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%2f53271060%2fhow-do-you-aggregate-the-deltas-between-two-arbitrary-datetime-columns%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

Bressuire

Vorschmack

Quarantine