Solved

Monthly Totals by Salesperson in Navicat Report

Posted on 2011-03-15
4
1,267 Views
Last Modified: 2012-08-13
I'm writing a report in Navicat that will query a online mysql database.

I currently have a report that uses the following query to give me a total testimonial count by salesperson for the year 2010 and this works good.  I need to also get a monthly total for each salesperson and keep the grand total as well.

Here is the query I'm currently using and it works good to give me a grand total. I have fields other than these but I'm assuming we can do some kind of a sum on each sales persons name then total by month.


SELECT
saleperson AS 'Sales Person',
count(saleperson) AS 'Testimonial Count'
From testimonial
WHERE recordtime >= '2010/01/01'
AND recordtime <= '2010/12/31'
Group by saleperson
Order by count(saleperson)Desc;

Open in new window

0
Comment
Question by:markj72000
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:fibo
ID: 35353449
Does
SELECT
saleperson AS 'Sales Person', substr(recordtime,6,2) as Month,
count(saleperson) AS 'Testimonial Count'
From testimonial
WHERE recordtime >= '2010/01/01'
AND recordtime <= '2010/12/31'
Group by saleperson, Month
Order by count(saleperson)Desc, Month;

Open in new window

makes you closer?
0
 

Author Comment

by:markj72000
ID: 35377848
This was close to what i wanted but i only wanted to list each salesperson once then have a column for each month "name" with a total for that month. I figured out a way to do this in excel after linking the table to a sheet then performing some calculations but I would still prefer to figure out a way to do this via sql if possible.

thanks

Mark
0
 
LVL 29

Accepted Solution

by:
fibo earned 500 total points
ID: 35381287
So you want to make a "crosstab" where each row belongs to a salesperson (whose name is in first column) and where months are in columns.

This is in fact a 3-steps queries: first find the names of columns, then create a new MySQL table, then fill in the totals in this table.
The first step is easy, usually just a single SELECT DISTINCT over the column which contains the attributes names; in your case these could also be generated directly without any SQL.
Then the 2nd step: create a MySQL table with field names coming from columns names (including the first 'person name') and  create one record per salesperson
Just remains to UPDATE the table column after column
Crosstab are not easy to make just within MySQL and this has created lots of discussion leading to some solutions that seem to work... but personally this is where I create the table with PHP: collecting data from MySQL and handling it to fill all the cells in the table.

Some references that might help you if you really want to make it in MySQL only:
http://rpbouman.blogspot.com/2005/10/creating-crosstabs-in-mysql.html started a long time ago, but still valid; uses stored procedures

0
 

Author Comment

by:markj72000
ID: 35415429
Thanks for the info Fibo and I kind of thought there would be a lot to getting sql to do this. I ended up creating a data connection in excel to pull in the table then did part of the report with the distinct query and the rest with a excel formula.

Thanks again for your help and sorry for the delay, I've been a little sick.

Mark
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

10 Experts available now in Live!

Get 1:1 Help Now