Solved

MySql - How can I create a summary report of registrations per week.

Posted on 2009-04-02
6
444 Views
Last Modified: 2012-05-06
Hi

I have a mysql database with a list of customers and a time stamp to show when they registered. I would like to produce a report that shows.

Week  |  Date Starting    |  Registrations

1         |  1/April/2008     |     20
2         |  8/April/2008     |     15
3         | 15/April/2008    |     6

The field is called "tStamp".  Could somebody show me how to write the query?

Kind Regards,

Adrian Smith

0
Comment
Question by:lwfuk
[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
  • 4
  • 2
6 Comments
 
LVL 10

Expert Comment

by:cyberstalker
ID: 24052087
Something like this perhaps?
SELECT      WEEK(`tStamp`) AS `Week`,
            DATE_ADD(`tStamp`, INTERVAL(1-DAYOFWEEK(`tStamp`)) DAY) AS `Date Starting`,
            COUNT(*) AS `Registrations`
FROM        **TABLENAME**
GROUP BY    WEEK(`tStamp`);

Open in new window

0
 

Author Comment

by:lwfuk
ID: 24052143
Hi Cyberstalker

Your a genius!

Would you know how to remove the time section and just show the date?

Kind Regards,

Adrian

Sample Output.

Week       Date Starting       Registrations
1       2009-01-04 02:53:28       4
2       2009-01-11 04:02:23       6
3       2009-01-18 09:55:13       10
4       2009-01-25 03:03:07       4
5       2009-02-01 06:55:44       11
6       2009-02-08 02:41:04       11
7       2009-02-15 05:38:17       5
8       2008-02-24 17:00:00       6
0
 

Author Comment

by:lwfuk
ID: 24052277
PS Cyberstalker

Could you also show me how to make the data:

ORDER BY: Date Starting, Assending

At the moment the dates and weeks are not ordered. For example Week 8 is in 2008 and week 1 is in 2009.

Kind Regards,

Adrian Smith
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 10

Accepted Solution

by:
cyberstalker earned 125 total points
ID: 24052412
Of course. You can use DATE_FORMAT to format the way you see the date. Ordering can be done easily with order by on the tStamp field.
SELECT      WEEK(`tStamp`) AS `Week`,
            DATE_FORMAT(DATE_ADD(`tStamp`, INTERVAL(1-DAYOFWEEK(`tStamp`)) DAY), '%Y-%m-%d') AS `Date Starting`,
            COUNT(*) AS `Registrations`
FROM        **TABLENAME**
GROUP BY    WEEK(`tStamp`)
ORDER BY    `tStamp`;

Open in new window

0
 

Author Closing Comment

by:lwfuk
ID: 31565868
Many Thanks.

Adrian Smith
0
 

Author Comment

by:lwfuk
ID: 24057467
For anybody finding this post, I did a small tweek.

The query above is fine if your data is less than 1 year.

If it's more than 1 year all of the data from the individual weeks are added together. For example all of the registrations from week 1 of 2007, 2008 and 2009 are added together.

This is corrected by grouping by the date starting field.



 
SELECT 	WEEK( `tStamp` ) AS `Week` , 
		COUNT( * ) AS `Registrations` , 
		DATE_FORMAT( DATE_ADD( `tStamp` , INTERVAL( 1 - DAYOFWEEK( `tStamp` ) )DAY ) , '%d-%m-%Y' ) AS `Date Starting`
FROM 	home_front_contacts
GROUP 	BY `Date Starting`
ORDER 	BY `tStamp`
LIMIT 	1 , 100

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

630 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