How can one generate a view, with tallied sums from a secondary view?
I have a view, built to generate a validation report on a mission critical cron that runs weekly. The view show weekly runs,
Sum of total accounts credited,
( some credit goes to SO buddies for my results!) , sum of OPEN accounts, sum of closed accounts, basic stuff.
mysql> select * from view_command_OPEN_CLOSED_tally limit 5;
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| created | total_accounts_credited | total_amount_credited | total_open_credited | total_amount_open | total_closed_credited| total_amount_closed|
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| 2019-01-19 00:00:00 | 18175 | 3173.68 | 16953 | 7063.68 | 1222 | 110.00 |
| 2019-01-12 00:00:00 | 18135 | 4768.43 | 17053 | 9358.43 | 1082 | 410.00 |
| 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | 1497.75 | 0 | 0.00 |
| 2019-01-09 09:20:55 | 51 | 933.50 | 50 | 915.75 | 1 | 17.75 |
| 2019-01-08 16:45:14 | 10 | 187.50 | 10 | 187.50 | 0 | 0.00 |
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
I assumed I could easily generate a second view from this one, with a subquery that would show difference in percentage from previous week. IE:
Created, total_accounts_credited, difference_from_last_week_in_percent, total_ammount_credited, difference_in_percent_from_last_week, etc ...
I cannot get my previous week results to match the data and calculate percentage. The resultset dispays some row of previous
The queries I tried all revolved around a basic left join, but for some reason the "previous week" result is not shown at appropiate location.
I am obviously doing something direspectful to MySQL in my approach, and in response, it is spitting me out an incoherent resultset.
mysql> SELECT vtally.created,
-> vtally.total_accounts_credited,
-> vtally2.total_accounts_credited `previous`,
-> vtally.total_open_credited ,
-> vtally2.total_open_credited `previous`,
-> vtally.total_closed_credited,
-> vtally2.total_closed_credited`previous`
-> FROM view_command_OPEN_CLOSED_tally vtally
-> LEFT JOIN view_command_OPEN_CLOSED_tally vtally2
-> ON vtally.created = vtally2.created - INTERVAL 7 DAY
-> GROUP BY
-> DATE(vtally.created)
-> ORDER BY vtally.created DESC LIMIT 2;
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | NULL | 6953 | NULL | 222 | NULL |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
The resultset above is an example,
I would have expected result to be as follows, with every previous column holding previous's week result of column to its left:
(top row only, disregard bottom row)
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | 8135 | 6953 | 7053 | 222 | 082 |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
What am I missing to have proper "previous" total in proper column?
I believe it could be the Group BY, but have no idea where to go from here it seems tried all options.
mysql query mysql-5.7
add a comment |
I have a view, built to generate a validation report on a mission critical cron that runs weekly. The view show weekly runs,
Sum of total accounts credited,
( some credit goes to SO buddies for my results!) , sum of OPEN accounts, sum of closed accounts, basic stuff.
mysql> select * from view_command_OPEN_CLOSED_tally limit 5;
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| created | total_accounts_credited | total_amount_credited | total_open_credited | total_amount_open | total_closed_credited| total_amount_closed|
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| 2019-01-19 00:00:00 | 18175 | 3173.68 | 16953 | 7063.68 | 1222 | 110.00 |
| 2019-01-12 00:00:00 | 18135 | 4768.43 | 17053 | 9358.43 | 1082 | 410.00 |
| 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | 1497.75 | 0 | 0.00 |
| 2019-01-09 09:20:55 | 51 | 933.50 | 50 | 915.75 | 1 | 17.75 |
| 2019-01-08 16:45:14 | 10 | 187.50 | 10 | 187.50 | 0 | 0.00 |
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
I assumed I could easily generate a second view from this one, with a subquery that would show difference in percentage from previous week. IE:
Created, total_accounts_credited, difference_from_last_week_in_percent, total_ammount_credited, difference_in_percent_from_last_week, etc ...
I cannot get my previous week results to match the data and calculate percentage. The resultset dispays some row of previous
The queries I tried all revolved around a basic left join, but for some reason the "previous week" result is not shown at appropiate location.
I am obviously doing something direspectful to MySQL in my approach, and in response, it is spitting me out an incoherent resultset.
mysql> SELECT vtally.created,
-> vtally.total_accounts_credited,
-> vtally2.total_accounts_credited `previous`,
-> vtally.total_open_credited ,
-> vtally2.total_open_credited `previous`,
-> vtally.total_closed_credited,
-> vtally2.total_closed_credited`previous`
-> FROM view_command_OPEN_CLOSED_tally vtally
-> LEFT JOIN view_command_OPEN_CLOSED_tally vtally2
-> ON vtally.created = vtally2.created - INTERVAL 7 DAY
-> GROUP BY
-> DATE(vtally.created)
-> ORDER BY vtally.created DESC LIMIT 2;
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | NULL | 6953 | NULL | 222 | NULL |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
The resultset above is an example,
I would have expected result to be as follows, with every previous column holding previous's week result of column to its left:
(top row only, disregard bottom row)
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | 8135 | 6953 | 7053 | 222 | 082 |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
What am I missing to have proper "previous" total in proper column?
I believe it could be the Group BY, but have no idea where to go from here it seems tried all options.
mysql query mysql-5.7
add a comment |
I have a view, built to generate a validation report on a mission critical cron that runs weekly. The view show weekly runs,
Sum of total accounts credited,
( some credit goes to SO buddies for my results!) , sum of OPEN accounts, sum of closed accounts, basic stuff.
mysql> select * from view_command_OPEN_CLOSED_tally limit 5;
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| created | total_accounts_credited | total_amount_credited | total_open_credited | total_amount_open | total_closed_credited| total_amount_closed|
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| 2019-01-19 00:00:00 | 18175 | 3173.68 | 16953 | 7063.68 | 1222 | 110.00 |
| 2019-01-12 00:00:00 | 18135 | 4768.43 | 17053 | 9358.43 | 1082 | 410.00 |
| 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | 1497.75 | 0 | 0.00 |
| 2019-01-09 09:20:55 | 51 | 933.50 | 50 | 915.75 | 1 | 17.75 |
| 2019-01-08 16:45:14 | 10 | 187.50 | 10 | 187.50 | 0 | 0.00 |
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
I assumed I could easily generate a second view from this one, with a subquery that would show difference in percentage from previous week. IE:
Created, total_accounts_credited, difference_from_last_week_in_percent, total_ammount_credited, difference_in_percent_from_last_week, etc ...
I cannot get my previous week results to match the data and calculate percentage. The resultset dispays some row of previous
The queries I tried all revolved around a basic left join, but for some reason the "previous week" result is not shown at appropiate location.
I am obviously doing something direspectful to MySQL in my approach, and in response, it is spitting me out an incoherent resultset.
mysql> SELECT vtally.created,
-> vtally.total_accounts_credited,
-> vtally2.total_accounts_credited `previous`,
-> vtally.total_open_credited ,
-> vtally2.total_open_credited `previous`,
-> vtally.total_closed_credited,
-> vtally2.total_closed_credited`previous`
-> FROM view_command_OPEN_CLOSED_tally vtally
-> LEFT JOIN view_command_OPEN_CLOSED_tally vtally2
-> ON vtally.created = vtally2.created - INTERVAL 7 DAY
-> GROUP BY
-> DATE(vtally.created)
-> ORDER BY vtally.created DESC LIMIT 2;
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | NULL | 6953 | NULL | 222 | NULL |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
The resultset above is an example,
I would have expected result to be as follows, with every previous column holding previous's week result of column to its left:
(top row only, disregard bottom row)
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | 8135 | 6953 | 7053 | 222 | 082 |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
What am I missing to have proper "previous" total in proper column?
I believe it could be the Group BY, but have no idea where to go from here it seems tried all options.
mysql query mysql-5.7
I have a view, built to generate a validation report on a mission critical cron that runs weekly. The view show weekly runs,
Sum of total accounts credited,
( some credit goes to SO buddies for my results!) , sum of OPEN accounts, sum of closed accounts, basic stuff.
mysql> select * from view_command_OPEN_CLOSED_tally limit 5;
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| created | total_accounts_credited | total_amount_credited | total_open_credited | total_amount_open | total_closed_credited| total_amount_closed|
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
| 2019-01-19 00:00:00 | 18175 | 3173.68 | 16953 | 7063.68 | 1222 | 110.00 |
| 2019-01-12 00:00:00 | 18135 | 4768.43 | 17053 | 9358.43 | 1082 | 410.00 |
| 2019-01-10 09:00:27 | 80 | 1497.75 | 80 | 1497.75 | 0 | 0.00 |
| 2019-01-09 09:20:55 | 51 | 933.50 | 50 | 915.75 | 1 | 17.75 |
| 2019-01-08 16:45:14 | 10 | 187.50 | 10 | 187.50 | 0 | 0.00 |
+---------------------+-------------------------+-----------------------+---------------------+-------------------+----------------------+--------------------+
I assumed I could easily generate a second view from this one, with a subquery that would show difference in percentage from previous week. IE:
Created, total_accounts_credited, difference_from_last_week_in_percent, total_ammount_credited, difference_in_percent_from_last_week, etc ...
I cannot get my previous week results to match the data and calculate percentage. The resultset dispays some row of previous
The queries I tried all revolved around a basic left join, but for some reason the "previous week" result is not shown at appropiate location.
I am obviously doing something direspectful to MySQL in my approach, and in response, it is spitting me out an incoherent resultset.
mysql> SELECT vtally.created,
-> vtally.total_accounts_credited,
-> vtally2.total_accounts_credited `previous`,
-> vtally.total_open_credited ,
-> vtally2.total_open_credited `previous`,
-> vtally.total_closed_credited,
-> vtally2.total_closed_credited`previous`
-> FROM view_command_OPEN_CLOSED_tally vtally
-> LEFT JOIN view_command_OPEN_CLOSED_tally vtally2
-> ON vtally.created = vtally2.created - INTERVAL 7 DAY
-> GROUP BY
-> DATE(vtally.created)
-> ORDER BY vtally.created DESC LIMIT 2;
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | NULL | 6953 | NULL | 222 | NULL |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
The resultset above is an example,
I would have expected result to be as follows, with every previous column holding previous's week result of column to its left:
(top row only, disregard bottom row)
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| created | total_accounts_credited | previous | total_open_credited | previous | total_closed_credited | previous |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
| 2019-01-19 00:00:00 | 8175 | 8135 | 6953 | 7053 | 222 | 082 |
| 2019-01-12 00:00:00 | 8135 | 8175 | 7053 | 6953 | 082 | 1222 |
+---------------------+-------------------------+----------+---------------------+----------+----------------------+----------+
What am I missing to have proper "previous" total in proper column?
I believe it could be the Group BY, but have no idea where to go from here it seems tried all options.
mysql query mysql-5.7
mysql query mysql-5.7
asked 8 mins ago
stefgosselinstefgosselin
1104
1104
add a comment |
add a comment |
0
active
oldest
votes
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
});
}
});
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%2f227595%2fhow-can-one-generate-a-view-with-tallied-sums-from-a-secondary-view%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 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%2f227595%2fhow-can-one-generate-a-view-with-tallied-sums-from-a-secondary-view%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