简体   繁体   中英

Count totals per month per salesman

I have a mySQL table with orders. Each table row includes order date, amount and sales person

 order_id   | 
 amount     |  
 saleman_id | 
 order_date

I would like to query it so I can generate a HTML table with a report looking lke this.

 Year | Month | Total | Total for salesman 1 | Total for salesman 2 | etc...
 2014 | 10    | 5000  | 2000                 | 3000                 | etc...
 2014 | 11    | 6000  | 5000                 | 1000                 | etc...

This is already partially working, meaning that I have a query that allows me to create a report showing the total for every month from all salesmen but I'd like to know if there is a way to achive what I want in a single SQL query and if it's worth it performance whise. Otherwise I can just query the result for each salesman in the PHP while loop that generates th table that I already have.

Mu current query is:

SELECT 
YEAR(ord_date) AS year, 
MONTH(ord_date) AS month,  
ROUND(SUM(...colum calculations...),2) AS sales, 
COUNT(o.ord_id) AS count_orders 
FROM 
orders_ord AS o 
GROUP BY 
year, month
ORDER BY year DESC, month DESC;

I am using PHP 5.4 with the classic mysql module & mySQL5.5

You can do what you want using with rollup :

SELECT YEAR(ord_date) AS year, MONTH(ord_date) AS month, repid, MONTHNAME(ord_date) AS monthname, 
       ROUND(SUM(...colum calculations...),2) AS profit,
       ROUND(SUM(...colum calculations...),2) AS sales, 
       COUNT(o.ord_id) AS count 
FROM orders_ord  o 
GROUP BY year, month
ORDER BY year DESC, month desc, repid with rollup;

However, because of the nature of the rollup, I would suggest combining the year and month in the group by :

SELECT YEAR(ord_date) AS year, MONTH(ord_date) AS month, repid, MONTHNAME(ord_date) AS monthname, 
       ROUND(SUM(...colum calculations...),2) AS profit,
       ROUND(SUM(...colum calculations...),2) AS sales, 
       COUNT(o.ord_id) AS count 
FROM orders_ord  o 
GROUP BY YEAR(ord_date) * 100 + MONTH(ord_date) desc, repid with rollup;

This will put only summary rows for individual months, rather than summaries for both the year and for the month.

Without seeing your show create table it hard to give you exact query. Genarelly speeking I like to do all the work in SQL not in PHP. However, if you have a grid of data and you want to show a subtotal or a grand total then use PHP to add the rows up.

Anyhow, here are some queries that will help you

This query will give you the rounded profit and rounded sales

SELECT 
rep_id,
YEAR(ord_date) AS year, 
MONTH(ord_date) AS month,  
MONTHNAME(ord_date) AS monthname, 
ROUND(SUM(...colum calculations...),2) AS profit,
ROUND(SUM(...colum calculations...),2) AS sales, 
COUNT(o.ord_id) AS count 
FROM 
orders_ord AS o 
GROUP BY rep_id, year, month
ORDER BY year DESC, month DESC;

Let the database do all, it is good at. (And save all the additional trips to the database, when filling in the gaps from PHP.)

SELECT 
YEAR(ord_date) AS year, 
MONTH(ord_date) AS month,  
MONTHNAME(ord_date) AS monthname, 
saleman_id,
ROUND(SUM(...colum calculations...),2) AS profit,
ROUND(SUM(...colum calculations...),2) AS sales, 
COUNT(o.ord_id) AS count 
FROM 
orders_ord 
GROUP BY  year, month, saleman_id

UNION ALL

SELECT 
YEAR(ord_date) AS year, 
MONTH(ord_date) AS month,  
MONTHNAME(ord_date) AS monthname,
'zzz Total zzz', 
ROUND(SUM(...colum calculations...),2) AS profit,
ROUND(SUM(...colum calculations...),2) AS sales, 
COUNT(o.ord_id) AS count 
FROM 
orders_ord 
GROUP BY  year, month
ORDER BY year DESC, month DESC;

To be adjusted, if year totals are really not to be provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM