Solved

Monthly Totals by Salesperson in Navicat Report

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

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://…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 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