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
copyraw
-- 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)
  1.  -- CURRENT: count todays 
  2.  SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(NOW())
  3.   
  4.  -- BEFORE: count yesterdays total 
  5.  SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))
  6.   
  7.  -- TREND: count yesterdays from midnight (00:00) up to the same time today 
  8.  SELECT COUNT(id) FROM Table1 WHERE DATE(registerDate)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND TIME(registerDate) 

Yesterweeks
copyraw
-- 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)
  1.  -- CURRENT: count this weeks (no parameter so our week starts with Sundays) 
  2.  SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(NOW())
  3.   
  4.  -- BEFORE: count yesterweeks total 
  5.  SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK))
  6.   
  7.  -- TREND: count yesterweeks from the first day to the same day today during that week 
  8.  SELECT COUNT(id) FROM Table1 WHERE YEARWEEK(registerDate)=YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK)) AND WEEKDAY(registerDate) 

Yestermonths
copyraw
-- 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)
  1.  -- CURRENT: count this months 
  2.  SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(NOW()) AND MONTH(registerDate)=MONTH(NOW())
  3.   
  4.  -- BEFORE: count yestermonths total 
  5.  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))
  6.   
  7.  -- TREND: count yestermonths from the first day to the same day today during that month 
  8.  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
copyraw
-- 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)
  1.  -- CURRENT: count this years 
  2.  SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(NOW())
  3.   
  4.  -- BEFORE: count yesteryears total 
  5.  SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR))
  6.   
  7.  -- TREND: count yesteryears from the first day to the same day today during that year 
  8.  SELECT COUNT(id) FROM Table1 WHERE YEAR(registerDate)=YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR)) AND DAYOFYEAR(registerDate) 

Category: MySQL :: Article: 397

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.