Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

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

Posted on 2008-06-26
6
Medium Priority
?
1,035 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 20

Accepted Solution

by:
virmaior earned 2000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

647 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