Solved

Monthly Totals by Salesperson in Navicat Report

Posted on 2011-03-15
4
1,271 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
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…

910 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

26 Experts available now in Live!

Get 1:1 Help Now