I recently made a joomla module that displays the lastest members to signup. It goes a little further and counts activated accounts for the past day, week, month and year (the below examples count all accounts irrespective of being activated or not). It needs to pick up trends as well and compare for example todays, with yesterdays up to the same hour.
How?
I used to use a lot more PHP but since becoming an analyst, I do more at the database level now. What follows should be usable mySQL statements to get all the numbers:
Yesterdays
-- CURRENT: count todays SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(NOW()); -- BEFORE: count yesterdays total SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)); -- TREND: count yesterdays from midnight (00:00) up to the same time today SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND TIME(registerDate)
- -- CURRENT: count todays
- SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(NOW());
- -- BEFORE: count yesterdays total
- SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY));
- -- TREND: count yesterdays from midnight (00:00) up to the same time today
- SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND TIME(registerDate)
Yesterweeks
-- CURRENT: count this weeks (no parameter so our week starts with Sundays) SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(NOW()); -- BEFORE: count yesterweeks total SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK)); -- TREND: count yesterweeks from the first day to the same day today during that week SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK)) AND WEEKDAY(registerDate)
- -- CURRENT: count this weeks (no parameter so our week starts with Sundays)
- SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(NOW());
- -- BEFORE: count yesterweeks total
- SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK));
- -- TREND: count yesterweeks from the first day to the same day today during that week
- SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK)) AND WEEKDAY(registerDate)
Yestermonths
-- CURRENT: count this months SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(NOW()) AND MONTH(registerDate)=MONTH(NOW()); -- BEFORE: count yestermonths total SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND MONTH(registerDate)=MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)); -- TREND: count yestermonths from the first day to the same day today during that month SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND MONTH(registerDate)=MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND DAYOFMONTH(registerDate)
- -- CURRENT: count this months
- SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(NOW()) AND MONTH(registerDate)=MONTH(NOW());
- -- BEFORE: count yestermonths total
- SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND MONTH(registerDate)=MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH));
- -- TREND: count yestermonths from the first day to the same day today during that month
- SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND MONTH(registerDate)=MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND DAYOFMONTH(registerDate)
Yesteryears
-- CURRENT: count this years SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(NOW()); -- BEFORE: count yesteryears total SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR)); -- TREND: count yesteryears from the first day to the same day today during that year SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR)) AND DAYOFYEAR(registerDate)
- -- CURRENT: count this years
- SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(NOW());
- -- BEFORE: count yesteryears total
- SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR));
- -- TREND: count yesteryears from the first day to the same day today during that year
- SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR)) AND DAYOFYEAR(registerDate)