Thursday, May 17, 2012

MySQL last year week month day trend periods

Why?
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)<=TIME(NOW());

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)<=WEEKDAY(NOW());

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)<=DAYOFMONTH(NOW());

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)<=DAYOFYEAR(NOW());

Add comment

Please note: all comments are reviewed before being published.


Security code
Refresh

Member Login

Joes Latest Members

A huge WELCOME goes to today's newest members:

  • infonat
Member Signups (Activated)
BeforeCurrentTrend
Day21=
Week105ê
Month11224ê
Year2,265612ê

Member Stats
52 guests are currently online.
1,178 members are still deciding.
There are 5,935 members in total.

Latest Comments

Paypal Donations

Want to support my work? Any donation is a blessing :c)