Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-02
6
Medium Priority
?
448 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 500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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