Solved

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

Posted on 2009-04-02
6
421 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

This article describes some very basic things about SQL Server filegroups.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…

770 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