Learn how to a build a cloud-first strategyRegister Now

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,058 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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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