Solved

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

Posted on 2008-06-26
6
953 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now