Solved

Monthly Totals by Salesperson in Navicat Report

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
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
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 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