简体   繁体   中英

A query to calculate registered users per day in a range of 3.5 months

I have a table called "users" where users register and the date of registration is saved as "created_on". I am looking to write a query that displays how many users have registered on our website daily from 1st of March 2017 till 15th of June 2017.

Unfortunately I can not.

A result that shows like this:

Date           Count
01-03-2017     232
02-03-2017     422
03-03-2017     531
...

This problem is a good example of where a calendar table would come in handy. In the query below, I assume that there exists a table containing all the dates from 1st March 2017 until and including 15 June 2017. An inline form of this table would look something like this:

SELECT '2017-03-01' AS Date UNION ALL
SELECT '2017-03-02' UNION ALL
-- ...
SELECT '2017-06-15'

Assuming you have a calendar table, you can just left join it to your current table to get the counts:

SELECT
    c.Date, COUNT(t.created_on) AS signup_count
FROM calendar c
LEFT JOIN yourTable t
    ON c.Date = t.created_on
GROUP BY Date

Note that a calendar table may be necessary here, because there could be days where no one signed up. In this case, your original table itself does not contain enough information to generate the output you expect.

SELECT created_on,COUNT(*) 
FROM users 
WHERE created_on BETWEEN '2017-03-01' AND '2017-06-15' 
GROUP BY DATE(created_on)

Try above query.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM