Group data by Custom Period Ranges Using a Reference DateIndexes involved in intervals collision...
Putting a vertical line in each Histogram using GraphicsGrid
Why do single electrical receptacles exist?
Is there a way to pause a running process on Linux systems and resume later?
Crack the bank account's password!
Is there any way to play D&D without a DM?
Tikz: Perpendicular FROM a line
How would an EMP effect spacesuits (and small-arms weapons)?
Explicit way to check whether a function was called from within the Window
Isn't a semicolon (';') needed after a function declaration in C++?
Is Screenshot Time-tracking Common?
How can I deduce the power of a capacitor from its datasheet?
A sequence of orthogonal projection in Hilbert space
How to deal with an underperforming subordinate?
Can I travel from country A to country B to country C without going back to country A?
What is an efficient way to digitize a family photo collection?
How to know if I am a 'Real Developer'
Is practicing on a digital piano harmful to an experienced piano player?
How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?
Is the percentage symbol a constant?
Promise.all returning empty objects
How do I add a strong "onion flavor" to the biryani (in restaurant style)?
In a post-apocalypse world, with no power and few survivors, would Satnav still work?
Is "accuse people to be racist" grammatical?
How can I prevent an oracle who can see into the past from knowing everything that has happened?
Group data by Custom Period Ranges Using a Reference Date
Indexes involved in intervals collision detection“GROUP BY” in ranges?MySQL Summary with UnionUsing composite indexes with date rangesHow do I prevent multiple row insert?Encrypt and decrypt text using the same keyDate and time period queryGet contiguous date ranges in grouped dataDatabase schema design to send unique messages to userA query for results based on date range
If for example, I have a table that looks like this:
+----+--------+---------------------+
| id | volume | createdAt |
+----+--------+---------------------+
| 1 | 0.11 | 2018-01-26 13:56:01 |
| 2 | 0.34 | 2018-01-28 14:22:12 |
| 3 | 0.22 | 2018-03-11 11:01:12 |
| 4 | 0.19 | 2018-04-13 12:12:12 |
| 5 | 0.12 | 2014-04-21 19:12:11 |
+----+--------+---------------------+
I want to perform a query that can accept starting point and then loop through a given number of days, and then group by that date range.
For instance, I'd like the result to look like this:
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
| ... | ... | ... |
+------------+------------+--------+
In essence, I want to be able to input a start_date
e.g 2018-04-25, a time_interval
e.g. 14, like in the illustration above and then the query will sum the volumes in that time range.
I know how to use INTERVAL
with the DATE_SUB()
and the DATE_ADD()
functions but I cannot figure out how to perform the loop I think is necessary.
Please help.
mysql
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
If for example, I have a table that looks like this:
+----+--------+---------------------+
| id | volume | createdAt |
+----+--------+---------------------+
| 1 | 0.11 | 2018-01-26 13:56:01 |
| 2 | 0.34 | 2018-01-28 14:22:12 |
| 3 | 0.22 | 2018-03-11 11:01:12 |
| 4 | 0.19 | 2018-04-13 12:12:12 |
| 5 | 0.12 | 2014-04-21 19:12:11 |
+----+--------+---------------------+
I want to perform a query that can accept starting point and then loop through a given number of days, and then group by that date range.
For instance, I'd like the result to look like this:
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
| ... | ... | ... |
+------------+------------+--------+
In essence, I want to be able to input a start_date
e.g 2018-04-25, a time_interval
e.g. 14, like in the illustration above and then the query will sum the volumes in that time range.
I know how to use INTERVAL
with the DATE_SUB()
and the DATE_ADD()
functions but I cannot figure out how to perform the loop I think is necessary.
Please help.
mysql
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
If for example, I have a table that looks like this:
+----+--------+---------------------+
| id | volume | createdAt |
+----+--------+---------------------+
| 1 | 0.11 | 2018-01-26 13:56:01 |
| 2 | 0.34 | 2018-01-28 14:22:12 |
| 3 | 0.22 | 2018-03-11 11:01:12 |
| 4 | 0.19 | 2018-04-13 12:12:12 |
| 5 | 0.12 | 2014-04-21 19:12:11 |
+----+--------+---------------------+
I want to perform a query that can accept starting point and then loop through a given number of days, and then group by that date range.
For instance, I'd like the result to look like this:
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
| ... | ... | ... |
+------------+------------+--------+
In essence, I want to be able to input a start_date
e.g 2018-04-25, a time_interval
e.g. 14, like in the illustration above and then the query will sum the volumes in that time range.
I know how to use INTERVAL
with the DATE_SUB()
and the DATE_ADD()
functions but I cannot figure out how to perform the loop I think is necessary.
Please help.
mysql
If for example, I have a table that looks like this:
+----+--------+---------------------+
| id | volume | createdAt |
+----+--------+---------------------+
| 1 | 0.11 | 2018-01-26 13:56:01 |
| 2 | 0.34 | 2018-01-28 14:22:12 |
| 3 | 0.22 | 2018-03-11 11:01:12 |
| 4 | 0.19 | 2018-04-13 12:12:12 |
| 5 | 0.12 | 2014-04-21 19:12:11 |
+----+--------+---------------------+
I want to perform a query that can accept starting point and then loop through a given number of days, and then group by that date range.
For instance, I'd like the result to look like this:
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
| ... | ... | ... |
+------------+------------+--------+
In essence, I want to be able to input a start_date
e.g 2018-04-25, a time_interval
e.g. 14, like in the illustration above and then the query will sum the volumes in that time range.
I know how to use INTERVAL
with the DATE_SUB()
and the DATE_ADD()
functions but I cannot figure out how to perform the loop I think is necessary.
Please help.
mysql
mysql
asked Apr 27 '18 at 14:48
AnonAnon
61
61
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 6 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I hope I understand this right and you can use this for at least a start.
SELECT x.startdate + INTERVAL (x.days + 1) DAY enddate,
x.startdate,
(SELECT sum(t.volumes)
FROM tab t
WHERE t.createdAt >= x.startdate
AND t.createdAt < x.startdate + INTERVAL (x.days + 1) DAY) volume
FROM (SELECT '2018-04-12' startdate,
12 days
UNION
SELECT '2018-03-29' startdate,
12 days
UNION
SELECT '2018-03-15' startdate,
12 days
UNION
SELECT '2018-03-01' startdate,
12 days
UNION
SELECT '2018-02-15' startdate,
12 days
UNION
SELECT '2018-02-01' startdate,
12 days
UNION
SELECT '2018-01-18' startdate,
12 days) x;
I haven't quite gotten on how you'd like to specify the dates and intervals though. Maybe you can clarify that, than I can probably edit this in.
add a comment |
There are 3 ways to get the list of date ranges.
Use a 'sequence table' that is available in MariaDB (but not in MySQL).
Create a table with a bunch of dates or integers from which to derive the desired dates.
Write a Stored Procedure with a
WHILE
loop that iterates through the desired dates.
add a comment |
mysql> CREATE TABLE volumes (id INT, volume DECIMAL(5,2), createdAt DATETIME);
Query OK, 0 rows affected (0.22 sec)
mysql>
mysql> INSERT INTO volumes
-> SELECT 1, 0.11, '2018-01-26 13:56:01' UNION ALL
-> SELECT 2, 0.34, '2018-01-28 14:22:12' UNION ALL
-> SELECT 3, 0.22, '2018-03-11 11:01:12' UNION ALL
-> SELECT 4, 0.19, '2018-04-13 12:12:12' UNION ALL
-> SELECT 5, 0.12, '2018-04-21 19:12:11' ;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> CREATE TABLE intervals (enddate DATE, startdate DATE);
Query OK, 0 rows affected (0.30 sec)
mysql>
mysql> INSERT INTO intervals
-> SELECT '2018-04-25', '2018-04-12' UNION ALL
-> SELECT '2018-04-11', '2018-03-29' UNION ALL
-> SELECT '2018-03-28', '2018-03-15' UNION ALL
-> SELECT '2018-03-14', '2018-03-01' UNION ALL
-> SELECT '2018-02-28', '2018-02-15' UNION ALL
-> SELECT '2018-02-14', '2018-02-01' UNION ALL
-> SELECT '2018-01-31', '2018-01-18' ;
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT i.enddate, i.startdate, COALESCE(SUM(v.volume), 0) volume
-> FROM intervals i
-> LEFT JOIN volumes v
-> ON v.createdAt BETWEEN i.startdate AND i.enddate
-> GROUP BY i.enddate DESC, i.startdate;
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
+------------+------------+--------+
7 rows in set (0.00 sec)
mysql>
add a comment |
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%2f205177%2fgroup-data-by-custom-period-ranges-using-a-reference-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I hope I understand this right and you can use this for at least a start.
SELECT x.startdate + INTERVAL (x.days + 1) DAY enddate,
x.startdate,
(SELECT sum(t.volumes)
FROM tab t
WHERE t.createdAt >= x.startdate
AND t.createdAt < x.startdate + INTERVAL (x.days + 1) DAY) volume
FROM (SELECT '2018-04-12' startdate,
12 days
UNION
SELECT '2018-03-29' startdate,
12 days
UNION
SELECT '2018-03-15' startdate,
12 days
UNION
SELECT '2018-03-01' startdate,
12 days
UNION
SELECT '2018-02-15' startdate,
12 days
UNION
SELECT '2018-02-01' startdate,
12 days
UNION
SELECT '2018-01-18' startdate,
12 days) x;
I haven't quite gotten on how you'd like to specify the dates and intervals though. Maybe you can clarify that, than I can probably edit this in.
add a comment |
I hope I understand this right and you can use this for at least a start.
SELECT x.startdate + INTERVAL (x.days + 1) DAY enddate,
x.startdate,
(SELECT sum(t.volumes)
FROM tab t
WHERE t.createdAt >= x.startdate
AND t.createdAt < x.startdate + INTERVAL (x.days + 1) DAY) volume
FROM (SELECT '2018-04-12' startdate,
12 days
UNION
SELECT '2018-03-29' startdate,
12 days
UNION
SELECT '2018-03-15' startdate,
12 days
UNION
SELECT '2018-03-01' startdate,
12 days
UNION
SELECT '2018-02-15' startdate,
12 days
UNION
SELECT '2018-02-01' startdate,
12 days
UNION
SELECT '2018-01-18' startdate,
12 days) x;
I haven't quite gotten on how you'd like to specify the dates and intervals though. Maybe you can clarify that, than I can probably edit this in.
add a comment |
I hope I understand this right and you can use this for at least a start.
SELECT x.startdate + INTERVAL (x.days + 1) DAY enddate,
x.startdate,
(SELECT sum(t.volumes)
FROM tab t
WHERE t.createdAt >= x.startdate
AND t.createdAt < x.startdate + INTERVAL (x.days + 1) DAY) volume
FROM (SELECT '2018-04-12' startdate,
12 days
UNION
SELECT '2018-03-29' startdate,
12 days
UNION
SELECT '2018-03-15' startdate,
12 days
UNION
SELECT '2018-03-01' startdate,
12 days
UNION
SELECT '2018-02-15' startdate,
12 days
UNION
SELECT '2018-02-01' startdate,
12 days
UNION
SELECT '2018-01-18' startdate,
12 days) x;
I haven't quite gotten on how you'd like to specify the dates and intervals though. Maybe you can clarify that, than I can probably edit this in.
I hope I understand this right and you can use this for at least a start.
SELECT x.startdate + INTERVAL (x.days + 1) DAY enddate,
x.startdate,
(SELECT sum(t.volumes)
FROM tab t
WHERE t.createdAt >= x.startdate
AND t.createdAt < x.startdate + INTERVAL (x.days + 1) DAY) volume
FROM (SELECT '2018-04-12' startdate,
12 days
UNION
SELECT '2018-03-29' startdate,
12 days
UNION
SELECT '2018-03-15' startdate,
12 days
UNION
SELECT '2018-03-01' startdate,
12 days
UNION
SELECT '2018-02-15' startdate,
12 days
UNION
SELECT '2018-02-01' startdate,
12 days
UNION
SELECT '2018-01-18' startdate,
12 days) x;
I haven't quite gotten on how you'd like to specify the dates and intervals though. Maybe you can clarify that, than I can probably edit this in.
answered Apr 28 '18 at 2:12
sticky bitsticky bit
1,925415
1,925415
add a comment |
add a comment |
There are 3 ways to get the list of date ranges.
Use a 'sequence table' that is available in MariaDB (but not in MySQL).
Create a table with a bunch of dates or integers from which to derive the desired dates.
Write a Stored Procedure with a
WHILE
loop that iterates through the desired dates.
add a comment |
There are 3 ways to get the list of date ranges.
Use a 'sequence table' that is available in MariaDB (but not in MySQL).
Create a table with a bunch of dates or integers from which to derive the desired dates.
Write a Stored Procedure with a
WHILE
loop that iterates through the desired dates.
add a comment |
There are 3 ways to get the list of date ranges.
Use a 'sequence table' that is available in MariaDB (but not in MySQL).
Create a table with a bunch of dates or integers from which to derive the desired dates.
Write a Stored Procedure with a
WHILE
loop that iterates through the desired dates.
There are 3 ways to get the list of date ranges.
Use a 'sequence table' that is available in MariaDB (but not in MySQL).
Create a table with a bunch of dates or integers from which to derive the desired dates.
Write a Stored Procedure with a
WHILE
loop that iterates through the desired dates.
answered May 16 '18 at 13:21
Rick JamesRick James
42.9k22259
42.9k22259
add a comment |
add a comment |
mysql> CREATE TABLE volumes (id INT, volume DECIMAL(5,2), createdAt DATETIME);
Query OK, 0 rows affected (0.22 sec)
mysql>
mysql> INSERT INTO volumes
-> SELECT 1, 0.11, '2018-01-26 13:56:01' UNION ALL
-> SELECT 2, 0.34, '2018-01-28 14:22:12' UNION ALL
-> SELECT 3, 0.22, '2018-03-11 11:01:12' UNION ALL
-> SELECT 4, 0.19, '2018-04-13 12:12:12' UNION ALL
-> SELECT 5, 0.12, '2018-04-21 19:12:11' ;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> CREATE TABLE intervals (enddate DATE, startdate DATE);
Query OK, 0 rows affected (0.30 sec)
mysql>
mysql> INSERT INTO intervals
-> SELECT '2018-04-25', '2018-04-12' UNION ALL
-> SELECT '2018-04-11', '2018-03-29' UNION ALL
-> SELECT '2018-03-28', '2018-03-15' UNION ALL
-> SELECT '2018-03-14', '2018-03-01' UNION ALL
-> SELECT '2018-02-28', '2018-02-15' UNION ALL
-> SELECT '2018-02-14', '2018-02-01' UNION ALL
-> SELECT '2018-01-31', '2018-01-18' ;
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT i.enddate, i.startdate, COALESCE(SUM(v.volume), 0) volume
-> FROM intervals i
-> LEFT JOIN volumes v
-> ON v.createdAt BETWEEN i.startdate AND i.enddate
-> GROUP BY i.enddate DESC, i.startdate;
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
+------------+------------+--------+
7 rows in set (0.00 sec)
mysql>
add a comment |
mysql> CREATE TABLE volumes (id INT, volume DECIMAL(5,2), createdAt DATETIME);
Query OK, 0 rows affected (0.22 sec)
mysql>
mysql> INSERT INTO volumes
-> SELECT 1, 0.11, '2018-01-26 13:56:01' UNION ALL
-> SELECT 2, 0.34, '2018-01-28 14:22:12' UNION ALL
-> SELECT 3, 0.22, '2018-03-11 11:01:12' UNION ALL
-> SELECT 4, 0.19, '2018-04-13 12:12:12' UNION ALL
-> SELECT 5, 0.12, '2018-04-21 19:12:11' ;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> CREATE TABLE intervals (enddate DATE, startdate DATE);
Query OK, 0 rows affected (0.30 sec)
mysql>
mysql> INSERT INTO intervals
-> SELECT '2018-04-25', '2018-04-12' UNION ALL
-> SELECT '2018-04-11', '2018-03-29' UNION ALL
-> SELECT '2018-03-28', '2018-03-15' UNION ALL
-> SELECT '2018-03-14', '2018-03-01' UNION ALL
-> SELECT '2018-02-28', '2018-02-15' UNION ALL
-> SELECT '2018-02-14', '2018-02-01' UNION ALL
-> SELECT '2018-01-31', '2018-01-18' ;
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT i.enddate, i.startdate, COALESCE(SUM(v.volume), 0) volume
-> FROM intervals i
-> LEFT JOIN volumes v
-> ON v.createdAt BETWEEN i.startdate AND i.enddate
-> GROUP BY i.enddate DESC, i.startdate;
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
+------------+------------+--------+
7 rows in set (0.00 sec)
mysql>
add a comment |
mysql> CREATE TABLE volumes (id INT, volume DECIMAL(5,2), createdAt DATETIME);
Query OK, 0 rows affected (0.22 sec)
mysql>
mysql> INSERT INTO volumes
-> SELECT 1, 0.11, '2018-01-26 13:56:01' UNION ALL
-> SELECT 2, 0.34, '2018-01-28 14:22:12' UNION ALL
-> SELECT 3, 0.22, '2018-03-11 11:01:12' UNION ALL
-> SELECT 4, 0.19, '2018-04-13 12:12:12' UNION ALL
-> SELECT 5, 0.12, '2018-04-21 19:12:11' ;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> CREATE TABLE intervals (enddate DATE, startdate DATE);
Query OK, 0 rows affected (0.30 sec)
mysql>
mysql> INSERT INTO intervals
-> SELECT '2018-04-25', '2018-04-12' UNION ALL
-> SELECT '2018-04-11', '2018-03-29' UNION ALL
-> SELECT '2018-03-28', '2018-03-15' UNION ALL
-> SELECT '2018-03-14', '2018-03-01' UNION ALL
-> SELECT '2018-02-28', '2018-02-15' UNION ALL
-> SELECT '2018-02-14', '2018-02-01' UNION ALL
-> SELECT '2018-01-31', '2018-01-18' ;
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT i.enddate, i.startdate, COALESCE(SUM(v.volume), 0) volume
-> FROM intervals i
-> LEFT JOIN volumes v
-> ON v.createdAt BETWEEN i.startdate AND i.enddate
-> GROUP BY i.enddate DESC, i.startdate;
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
+------------+------------+--------+
7 rows in set (0.00 sec)
mysql>
mysql> CREATE TABLE volumes (id INT, volume DECIMAL(5,2), createdAt DATETIME);
Query OK, 0 rows affected (0.22 sec)
mysql>
mysql> INSERT INTO volumes
-> SELECT 1, 0.11, '2018-01-26 13:56:01' UNION ALL
-> SELECT 2, 0.34, '2018-01-28 14:22:12' UNION ALL
-> SELECT 3, 0.22, '2018-03-11 11:01:12' UNION ALL
-> SELECT 4, 0.19, '2018-04-13 12:12:12' UNION ALL
-> SELECT 5, 0.12, '2018-04-21 19:12:11' ;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> CREATE TABLE intervals (enddate DATE, startdate DATE);
Query OK, 0 rows affected (0.30 sec)
mysql>
mysql> INSERT INTO intervals
-> SELECT '2018-04-25', '2018-04-12' UNION ALL
-> SELECT '2018-04-11', '2018-03-29' UNION ALL
-> SELECT '2018-03-28', '2018-03-15' UNION ALL
-> SELECT '2018-03-14', '2018-03-01' UNION ALL
-> SELECT '2018-02-28', '2018-02-15' UNION ALL
-> SELECT '2018-02-14', '2018-02-01' UNION ALL
-> SELECT '2018-01-31', '2018-01-18' ;
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT i.enddate, i.startdate, COALESCE(SUM(v.volume), 0) volume
-> FROM intervals i
-> LEFT JOIN volumes v
-> ON v.createdAt BETWEEN i.startdate AND i.enddate
-> GROUP BY i.enddate DESC, i.startdate;
+------------+------------+--------+
| enddate | startdate | volume |
+------------+------------+--------+
| 2018-04-25 | 2018-04-12 | 0.31 |
| 2018-04-11 | 2018-03-29 | 0.00 |
| 2018-03-28 | 2018-03-15 | 0.00 |
| 2018-03-14 | 2018-03-01 | 0.22 |
| 2018-02-28 | 2018-02-15 | 0.00 |
| 2018-02-14 | 2018-02-01 | 0.00 |
| 2018-01-31 | 2018-01-18 | 0.45 |
+------------+------------+--------+
7 rows in set (0.00 sec)
mysql>
answered May 16 '18 at 13:50
AkinaAkina
4,0741311
4,0741311
add a comment |
add a comment |
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%2f205177%2fgroup-data-by-custom-period-ranges-using-a-reference-date%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