Grouping Staff Sales Data

Using a two table inner join with field concatenation and grouping, data can be generated showing staff sales. It is necessary to perform a sum totalling the sales of green and blue widgets and also to perform a count on the individual sales to produce a useful report.

This data would be important to a sales team manager to assess performance. The data is sorted by widget sales in ascending order so that the highest sales are listed first.

Live results from the database are below:


staff_namesalestrans_grn_widgets_salestrans_blu_widgets_saleswidget_sales
Mr Charles Chuzzlewit1222800.0021600.0044400.00
Mr Billy Beaumont1216200.0022550.0038750.00
Miss Deirdrie Dawson1018000.0019400.0037400.00
Mrs Francesca Faversham917000.0018600.0035600.00
Miss Gina Ginola814500.0018000.0032500.00
Mrs Anthea Allen913500.0017200.0030700.00
Mr Harry Harrison67500.0019200.0026700.00
Mr Ernie Ecclestone69500.0016200.0025700.00


The above example shows output from data recorded from 1st January 2017 to the end of March.

MySQL Code

SELECT CONCAT(s.staff_salutation, ' ',s.staff_firstname, ' ', s.staff_surname) AS staffname,
COUNT(t.trans_cust_id) AS sales,
SUM(t.trans_grn_widgets_sales), SUM(t.trans_blu_widgets_sales),
SUM(t.trans_blu_widgets_sales + t.trans_grn_widgets_sales) AS widget_sales
FROM transactions AS t
INNER JOIN staff AS s
ON t.trans_staff_id = s.staff_id
GROUP BY s.staff_id
ORDER BY widget_sales DESC
T-SQL (MS SQL)

SELECT s.staff_salutation + ' ' + s.staff_firstname + ' ' + s.staff_surname AS staffname,
COUNT(t.trans_cust_id) AS sales,
SUM(t.trans_grn_widgets_sales) AS trans_grn_widgets_sales,
SUM(t.trans_blu_widgets_sales) AS trans_blu_widgets_sales,
SUM(t.trans_blu_widgets_sales + t.trans_grn_widgets_sales) AS widget_sales
FROM transactions t
INNER JOIN staff s
ON t.trans_staff_id = s.staff_id
GROUP BY s.staff_id, s.staff_surname, s.staff_firstname,s.staff_salutation
ORDER BY widget_sales DESC



Data below is filtered by a date range of 1st February 2017 to the end of February:


staff_namesalestrans_grn_widgets_salestrans_blu_widgets_saleswidget_sales
Mr Charles Chuzzlewit69500.0016800.0026300.00
Mr Billy Beaumont610000.0013750.0023750.00
Mr Harry Harrison45000.0014400.0019400.00
Mrs Anthea Allen47000.009400.0016400.00
Mr Ernie Ecclestone35000.009600.0014600.00
Miss Gina Ginola34500.006600.0011100.00
Mrs Francesca Faversham35500.005400.0010900.00
Miss Deirdrie Dawson34000.006600.0010600.00
MySQL Code

SELECT CONCAT(s.staff_salutation, ' ',s.staff_firstname, ' ', s.staff_surname) AS staffname,
COUNT(t.trans_cust_id) AS sales,
SUM(t.trans_grn_widgets_sales), SUM(t.trans_blu_widgets_sales),
SUM(t.trans_blu_widgets_sales + t.trans_grn_widgets_sales) AS widget_sales
FROM transactions AS t
INNER JOIN staff AS s
ON t.trans_staff_id = s.staff_id
WHERE (trans_date BETWEEN '2017-02-01 00:00:00' AND '2017-02-28 23:59:59')
GROUP BY s.staff_id
ORDER BY widget_sales DESC
T-SQL (MS-SQL)

SELECT s.staff_salutation + ' ' + s.staff_firstname + ' ' + s.staff_surname AS staffname,
COUNT(t.trans_cust_id) AS sales,
SUM(t.trans_grn_widgets_sales) AS trans_grn_widgets_sales,
SUM(t.trans_blu_widgets_sales) AS trans_blu_widgets_sales,
SUM(t.trans_blu_widgets_sales + t.trans_grn_widgets_sales) AS widget_sales
FROM transactions t
INNER JOIN staff s
ON t.trans_staff_id = s.staff_id
WHERE (trans_date BETWEEN '2017-02-01' AND '2017-02-28')
GROUP BY s.staff_id, s.staff_surname, s.staff_firstname,s.staff_salutation
ORDER BY widget_sales DESC


We can also set this query so that it only returns the results for the top three sales staff for the period of February 2017, based on widget sales.

This is achieved with a LIMIT clause in MySQL or by specifying a TOP value in T-SQL:


staff_namesalestrans_grn_widgets_salestrans_blu_widgets_saleswidget_sales
Mr Charles Chuzzlewit69500.0016800.0026300.00
Mr Billy Beaumont610000.0013750.0023750.00
Mr Harry Harrison45000.0014400.0019400.00


Click the button below to advance to the next stage


Next: >> Outer Join


Or use the above 'SQL Queries' dropdown menu to jump to any stage.