Solved

Create a  MySQL query to count records and GROUP BY every 7 days for a year?

Posted on 2008-06-26
6
978 Views
Last Modified: 2013-12-12
Hello,
I want to produce a report that counts the number of records that are entered into our database on a weekly basis. Is there a way, just using MySQL, that I can loop through a year's worth of dates, doing a total for each of the 52 weeks? Or do I have to capture all of the data and then parse in a "for" loop in PHP? If the latter, can you help with the PHP code? If the former, can you supply the SQL syntax?
Here is what I have so far:


SELECT DateEntered,Count(ID) AS Total FROM Applications WHERE DateEntered Between '2007-01-01' AND '2007-01-06' Group By DateEntered

Open in new window

0
Comment
Question by:aberns
  • 3
  • 3
6 Comments
 
LVL 20

Accepted Solution

by:
virmaior earned 500 total points
ID: 21875534
try this:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

SELECT DateEntered,Count(ID) AS Total FROM Applications WHERE 1 GROUP BY DATE_FORMAT(DateEntered,"%Y%v")

if that's not quite what you are looking for, then it starts getting more complicated
0
 

Author Comment

by:aberns
ID: 21875718
What I'm looking for is a total by 7 day intervals...so for example, the output would look like

DateRange Total
2008-01-01 - 2008-01-06: 145
2008-01-07 - 2008-01-13: 98
2008-01-14-2008-01-20: 123

And so on.....

Is there a way to Automatically Count and Group By every 7 days for a set number of weeks?
0
 

Author Comment

by:aberns
ID: 21875841
oh wait a minute...I just tried your query again and it works! I never knew about the %v parameter in DATE_FORMAT. Thanks......
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 20

Expert Comment

by:virmaior
ID: 21875864
did you look at what  the query does?

It aggregates by year and week.  A week is 7 days long.


if you have specific date ranges and know them, then you could do this:

CREATE TABLE IF NOT EXISTS `trick_table` (
  `trick_id` bigint(20) NOT NULL,
  `start_date` datetime NOT NULL,
  `end_date` datetime NOT NULL,
  PRIMARY KEY  (`trick_id`)
) ENGINE=MyISAM

INSERT INTO `trick_table` (start_date,end_date)
VALUES ('2008-01-01','2008-01-06') ....

SELECT (SELECT COUNT(ID) FROM applications WHERE DateEntered Between tt.start_date AND tt.end_date ) FROM trick_table tt
0
 

Author Comment

by:aberns
ID: 21876003
Yes, at first I didn't look carefully enough...but then realized that it aggregated by a week for a year. Thanks for your follow up.
Audrey
0
 
LVL 20

Expert Comment

by:virmaior
ID: 21876023
no problem
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question