Solved

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

Posted on 2009-04-02
6
432 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
Technology Partners: 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!

 
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword 2 97
Mongo DB 18 71
T-SQL: New to using transactions 9 54
Dump data from mysql to xls php 10 19
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

756 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