Solved

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

Posted on 2009-04-02
6
410 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now