MySql: Select active subscritions grouped by month
I have the following MySQL-Database-Table with subscriptions. Each subscription as a startdate and an enddate.
id | start_date | end_date
1 | 2017-01-01 | 2017-07-01
2 | 2017-01-15 | 2017-07-12
3 | 2017-02-01 | 2017-08-01
4 | 2017-03-01 | 2017-08-01
5 | 2017-03-12 | 2017-08-12
6 | 2017-03-30 | 2017-08-30
7 | 2017-05-01 | 2017-11-01
8 | 2017-06-01 | 2017-12-01
9 | 2017-07-01 | 2018-01-01
10 | 2017-08-01 | 2018-02-01
11 | 2018-01-01 | 2018-07-01
12 | 2018-02-01 | 2018-08-01
13 | 2018-03-01 | 2018-09-01
... | ... | ...
I would like to select all active subscritions within each month. Is this possible with one SQL-Query?
I would like to know: How many acitve subscriptions there were in January, February, March, etc.
For example the query for just June 2017 would be:
SELECT COUNT(*) FROM table
WHERE start_date <= '2017-06-30' AND
end_date >= '2017-06-01'
I hope my text is understandable.
mysql
add a comment |
I have the following MySQL-Database-Table with subscriptions. Each subscription as a startdate and an enddate.
id | start_date | end_date
1 | 2017-01-01 | 2017-07-01
2 | 2017-01-15 | 2017-07-12
3 | 2017-02-01 | 2017-08-01
4 | 2017-03-01 | 2017-08-01
5 | 2017-03-12 | 2017-08-12
6 | 2017-03-30 | 2017-08-30
7 | 2017-05-01 | 2017-11-01
8 | 2017-06-01 | 2017-12-01
9 | 2017-07-01 | 2018-01-01
10 | 2017-08-01 | 2018-02-01
11 | 2018-01-01 | 2018-07-01
12 | 2018-02-01 | 2018-08-01
13 | 2018-03-01 | 2018-09-01
... | ... | ...
I would like to select all active subscritions within each month. Is this possible with one SQL-Query?
I would like to know: How many acitve subscriptions there were in January, February, March, etc.
For example the query for just June 2017 would be:
SELECT COUNT(*) FROM table
WHERE start_date <= '2017-06-30' AND
end_date >= '2017-06-01'
I hope my text is understandable.
mysql
This will be tricky. You will need to use a Master calendar table, as well as a subscription may show up across multiple months.
– Madhur Bhaiya
Nov 21 '18 at 10:29
Please provide a relevant and minimal sample data showcasing your requirements (including edge cases), and corresponding expected output.
– Madhur Bhaiya
Nov 21 '18 at 10:30
add a comment |
I have the following MySQL-Database-Table with subscriptions. Each subscription as a startdate and an enddate.
id | start_date | end_date
1 | 2017-01-01 | 2017-07-01
2 | 2017-01-15 | 2017-07-12
3 | 2017-02-01 | 2017-08-01
4 | 2017-03-01 | 2017-08-01
5 | 2017-03-12 | 2017-08-12
6 | 2017-03-30 | 2017-08-30
7 | 2017-05-01 | 2017-11-01
8 | 2017-06-01 | 2017-12-01
9 | 2017-07-01 | 2018-01-01
10 | 2017-08-01 | 2018-02-01
11 | 2018-01-01 | 2018-07-01
12 | 2018-02-01 | 2018-08-01
13 | 2018-03-01 | 2018-09-01
... | ... | ...
I would like to select all active subscritions within each month. Is this possible with one SQL-Query?
I would like to know: How many acitve subscriptions there were in January, February, March, etc.
For example the query for just June 2017 would be:
SELECT COUNT(*) FROM table
WHERE start_date <= '2017-06-30' AND
end_date >= '2017-06-01'
I hope my text is understandable.
mysql
I have the following MySQL-Database-Table with subscriptions. Each subscription as a startdate and an enddate.
id | start_date | end_date
1 | 2017-01-01 | 2017-07-01
2 | 2017-01-15 | 2017-07-12
3 | 2017-02-01 | 2017-08-01
4 | 2017-03-01 | 2017-08-01
5 | 2017-03-12 | 2017-08-12
6 | 2017-03-30 | 2017-08-30
7 | 2017-05-01 | 2017-11-01
8 | 2017-06-01 | 2017-12-01
9 | 2017-07-01 | 2018-01-01
10 | 2017-08-01 | 2018-02-01
11 | 2018-01-01 | 2018-07-01
12 | 2018-02-01 | 2018-08-01
13 | 2018-03-01 | 2018-09-01
... | ... | ...
I would like to select all active subscritions within each month. Is this possible with one SQL-Query?
I would like to know: How many acitve subscriptions there were in January, February, March, etc.
For example the query for just June 2017 would be:
SELECT COUNT(*) FROM table
WHERE start_date <= '2017-06-30' AND
end_date >= '2017-06-01'
I hope my text is understandable.
mysql
mysql
edited Nov 21 '18 at 10:27
Madhur Bhaiya
19.6k62236
19.6k62236
asked Nov 21 '18 at 10:26
PhantomPhantom
114213
114213
This will be tricky. You will need to use a Master calendar table, as well as a subscription may show up across multiple months.
– Madhur Bhaiya
Nov 21 '18 at 10:29
Please provide a relevant and minimal sample data showcasing your requirements (including edge cases), and corresponding expected output.
– Madhur Bhaiya
Nov 21 '18 at 10:30
add a comment |
This will be tricky. You will need to use a Master calendar table, as well as a subscription may show up across multiple months.
– Madhur Bhaiya
Nov 21 '18 at 10:29
Please provide a relevant and minimal sample data showcasing your requirements (including edge cases), and corresponding expected output.
– Madhur Bhaiya
Nov 21 '18 at 10:30
This will be tricky. You will need to use a Master calendar table, as well as a subscription may show up across multiple months.
– Madhur Bhaiya
Nov 21 '18 at 10:29
This will be tricky. You will need to use a Master calendar table, as well as a subscription may show up across multiple months.
– Madhur Bhaiya
Nov 21 '18 at 10:29
Please provide a relevant and minimal sample data showcasing your requirements (including edge cases), and corresponding expected output.
– Madhur Bhaiya
Nov 21 '18 at 10:30
Please provide a relevant and minimal sample data showcasing your requirements (including edge cases), and corresponding expected output.
– Madhur Bhaiya
Nov 21 '18 at 10:30
add a comment |
2 Answers
2
active
oldest
votes
This approach uses a calendar table where each month is represented by the first day of that month. Then, we only need to left join this calendar table to your current table using overlapping ranges to find the number of subscriptions for each month.
SELECT
c.month,
COUNT(t.start_date) AS num_subscriptions
FROM
(
SELECT '2017-01-01' AS month UNION ALL
SELECT '2017-02-01' UNION ALL
...
SELECT '2018-12-01'
) c
LEFT JOIN yourTable t
ON c.month <= t.end_date AND LAST_DAY(c.month) >= t.start_date
GROUP BY
c.month;
Demo
Thank you very much! I will test it later and report back to you
– Phantom
Nov 21 '18 at 11:01
@Phantom There's no time like the present :-)
– Tim Biegeleisen
Nov 21 '18 at 11:02
Works great. Thank you very much!!!
– Phantom
Nov 21 '18 at 16:12
add a comment |
Use functions LAST_DAY(date) FIRST_DAY(date)
LAST_DAY is a valid function, but FIRST_DAY is not.
You can create a custom function.
DELIMITER ;;
CREATE FUNCTION FIRST_DAY(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN ADDDATE(LAST_DAY(SUBDATE(day, INTERVAL 1 MONTH)), 1);
END;;
DELIMITER ;
Answer copied from Stéphane's answer here
Sorry, but I don't understand in which way I should use those functions.
– Phantom
Nov 21 '18 at 11:03
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
});
}
});
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%2f53410000%2fmysql-select-active-subscritions-grouped-by-month%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
This approach uses a calendar table where each month is represented by the first day of that month. Then, we only need to left join this calendar table to your current table using overlapping ranges to find the number of subscriptions for each month.
SELECT
c.month,
COUNT(t.start_date) AS num_subscriptions
FROM
(
SELECT '2017-01-01' AS month UNION ALL
SELECT '2017-02-01' UNION ALL
...
SELECT '2018-12-01'
) c
LEFT JOIN yourTable t
ON c.month <= t.end_date AND LAST_DAY(c.month) >= t.start_date
GROUP BY
c.month;
Demo
Thank you very much! I will test it later and report back to you
– Phantom
Nov 21 '18 at 11:01
@Phantom There's no time like the present :-)
– Tim Biegeleisen
Nov 21 '18 at 11:02
Works great. Thank you very much!!!
– Phantom
Nov 21 '18 at 16:12
add a comment |
This approach uses a calendar table where each month is represented by the first day of that month. Then, we only need to left join this calendar table to your current table using overlapping ranges to find the number of subscriptions for each month.
SELECT
c.month,
COUNT(t.start_date) AS num_subscriptions
FROM
(
SELECT '2017-01-01' AS month UNION ALL
SELECT '2017-02-01' UNION ALL
...
SELECT '2018-12-01'
) c
LEFT JOIN yourTable t
ON c.month <= t.end_date AND LAST_DAY(c.month) >= t.start_date
GROUP BY
c.month;
Demo
Thank you very much! I will test it later and report back to you
– Phantom
Nov 21 '18 at 11:01
@Phantom There's no time like the present :-)
– Tim Biegeleisen
Nov 21 '18 at 11:02
Works great. Thank you very much!!!
– Phantom
Nov 21 '18 at 16:12
add a comment |
This approach uses a calendar table where each month is represented by the first day of that month. Then, we only need to left join this calendar table to your current table using overlapping ranges to find the number of subscriptions for each month.
SELECT
c.month,
COUNT(t.start_date) AS num_subscriptions
FROM
(
SELECT '2017-01-01' AS month UNION ALL
SELECT '2017-02-01' UNION ALL
...
SELECT '2018-12-01'
) c
LEFT JOIN yourTable t
ON c.month <= t.end_date AND LAST_DAY(c.month) >= t.start_date
GROUP BY
c.month;
Demo
This approach uses a calendar table where each month is represented by the first day of that month. Then, we only need to left join this calendar table to your current table using overlapping ranges to find the number of subscriptions for each month.
SELECT
c.month,
COUNT(t.start_date) AS num_subscriptions
FROM
(
SELECT '2017-01-01' AS month UNION ALL
SELECT '2017-02-01' UNION ALL
...
SELECT '2018-12-01'
) c
LEFT JOIN yourTable t
ON c.month <= t.end_date AND LAST_DAY(c.month) >= t.start_date
GROUP BY
c.month;
Demo
edited Nov 21 '18 at 10:41
answered Nov 21 '18 at 10:35
Tim BiegeleisenTim Biegeleisen
231k1396152
231k1396152
Thank you very much! I will test it later and report back to you
– Phantom
Nov 21 '18 at 11:01
@Phantom There's no time like the present :-)
– Tim Biegeleisen
Nov 21 '18 at 11:02
Works great. Thank you very much!!!
– Phantom
Nov 21 '18 at 16:12
add a comment |
Thank you very much! I will test it later and report back to you
– Phantom
Nov 21 '18 at 11:01
@Phantom There's no time like the present :-)
– Tim Biegeleisen
Nov 21 '18 at 11:02
Works great. Thank you very much!!!
– Phantom
Nov 21 '18 at 16:12
Thank you very much! I will test it later and report back to you
– Phantom
Nov 21 '18 at 11:01
Thank you very much! I will test it later and report back to you
– Phantom
Nov 21 '18 at 11:01
@Phantom There's no time like the present :-)
– Tim Biegeleisen
Nov 21 '18 at 11:02
@Phantom There's no time like the present :-)
– Tim Biegeleisen
Nov 21 '18 at 11:02
Works great. Thank you very much!!!
– Phantom
Nov 21 '18 at 16:12
Works great. Thank you very much!!!
– Phantom
Nov 21 '18 at 16:12
add a comment |
Use functions LAST_DAY(date) FIRST_DAY(date)
LAST_DAY is a valid function, but FIRST_DAY is not.
You can create a custom function.
DELIMITER ;;
CREATE FUNCTION FIRST_DAY(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN ADDDATE(LAST_DAY(SUBDATE(day, INTERVAL 1 MONTH)), 1);
END;;
DELIMITER ;
Answer copied from Stéphane's answer here
Sorry, but I don't understand in which way I should use those functions.
– Phantom
Nov 21 '18 at 11:03
add a comment |
Use functions LAST_DAY(date) FIRST_DAY(date)
LAST_DAY is a valid function, but FIRST_DAY is not.
You can create a custom function.
DELIMITER ;;
CREATE FUNCTION FIRST_DAY(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN ADDDATE(LAST_DAY(SUBDATE(day, INTERVAL 1 MONTH)), 1);
END;;
DELIMITER ;
Answer copied from Stéphane's answer here
Sorry, but I don't understand in which way I should use those functions.
– Phantom
Nov 21 '18 at 11:03
add a comment |
Use functions LAST_DAY(date) FIRST_DAY(date)
LAST_DAY is a valid function, but FIRST_DAY is not.
You can create a custom function.
DELIMITER ;;
CREATE FUNCTION FIRST_DAY(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN ADDDATE(LAST_DAY(SUBDATE(day, INTERVAL 1 MONTH)), 1);
END;;
DELIMITER ;
Answer copied from Stéphane's answer here
Use functions LAST_DAY(date) FIRST_DAY(date)
LAST_DAY is a valid function, but FIRST_DAY is not.
You can create a custom function.
DELIMITER ;;
CREATE FUNCTION FIRST_DAY(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN ADDDATE(LAST_DAY(SUBDATE(day, INTERVAL 1 MONTH)), 1);
END;;
DELIMITER ;
Answer copied from Stéphane's answer here
answered Nov 21 '18 at 10:32
Kristjan KicaKristjan Kica
2,2341927
2,2341927
Sorry, but I don't understand in which way I should use those functions.
– Phantom
Nov 21 '18 at 11:03
add a comment |
Sorry, but I don't understand in which way I should use those functions.
– Phantom
Nov 21 '18 at 11:03
Sorry, but I don't understand in which way I should use those functions.
– Phantom
Nov 21 '18 at 11:03
Sorry, but I don't understand in which way I should use those functions.
– Phantom
Nov 21 '18 at 11:03
add a comment |
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.
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%2f53410000%2fmysql-select-active-subscritions-grouped-by-month%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
This will be tricky. You will need to use a Master calendar table, as well as a subscription may show up across multiple months.
– Madhur Bhaiya
Nov 21 '18 at 10:29
Please provide a relevant and minimal sample data showcasing your requirements (including edge cases), and corresponding expected output.
– Madhur Bhaiya
Nov 21 '18 at 10:30