MySql: Select active subscritions grouped by month












1















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.










share|improve this question

























  • 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


















1















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.










share|improve this question

























  • 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
















1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





















  • 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














2 Answers
2






active

oldest

votes


















1














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;


enter image description here




Demo






share|improve this answer


























  • 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



















0














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






share|improve this answer
























  • Sorry, but I don't understand in which way I should use those functions.

    – Phantom
    Nov 21 '18 at 11:03











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%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









1














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;


enter image description here




Demo






share|improve this answer


























  • 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
















1














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;


enter image description here




Demo






share|improve this answer


























  • 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














1












1








1







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;


enter image description here




Demo






share|improve this answer















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;


enter image description here




Demo







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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













0














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






share|improve this answer
























  • Sorry, but I don't understand in which way I should use those functions.

    – Phantom
    Nov 21 '18 at 11:03
















0














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






share|improve this answer
























  • Sorry, but I don't understand in which way I should use those functions.

    – Phantom
    Nov 21 '18 at 11:03














0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud