Solved

Monthly Totals by Salesperson in Navicat Report

Posted on 2011-03-15
4
1,282 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need to create a new user to access our RDS/MySQL database 1 55
Excel - SQL export question 3 49
MS SQL Update query with connected table data 3 37
Complex MySQL Query 2 33
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.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 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