MySQL Count and SUM from second table with group by

Sirkong

I'm trying to get sales and quantity sale by crossing two tables, group by the first one and sum from the second one.

First table has sales/operations: id_sales, sales_rep Second table has sales details: id_sales_details, id_sales, quantity

What I need to know is how many operations had each sales_rep and what was the total quantity sum of all those sales.

This MySQL query gives me the first part:

SELECT sales.sales_rep, count(*) AS sales
from sales
Group by sales_rep
Order by sales DESC

What I cannot solve is how to add to that query the second part I need. The result should look something like:

sales_rep    sales    quantity
Claire             4          13
Peter              2          18
Mary              1           8
John              1           7

Here's a Fiddle to make things clearer: http://sqlfiddle.com/#!9/708234/5

user3317519
SELECT s.sales_rep, count(*) AS operations, sum(d.quantity)
  from sales s, sales_details d 
   where s.id_sales = d.id_sales 
   Group by s.sales_rep 
   Order by operations DESC;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL sum, count with group by and joins

From Dev

SUM after COUNT and Group By MYSQL

From Dev

Mysql for a given group type get count and sum from other tables

From Dev

MySQL: Return row count from second table using one query

From Dev

getting sum of count for all date wise data from table in mysql

From Dev

PHP PDO count and sum value from MySQL table

From Dev

MySql Sum and Count for simple table

From Dev

Mysql group by and having & sum of one column from table one

From Java

mysql sum and count 2 tables group by issue

From Dev

Cumulative Sum of group count in mysql query

From Dev

MySQL: select when count = sum & group by

From Dev

MySQL SUM and GROUP BY from AS column

From Dev

Python List - group by first item, count and sum second item

From Dev

linq order by from second table count

From Dev

MySQL join / sum value from one table colum with a count values from another

From Dev

join with count on a joined table with group clause in mysql

From Dev

Select * as well as count/sum from another table

From Dev

Count entries from second Table that match id from first Table

From Dev

How to DELETE With SELECT SUM() FROM Table GROUP BY

From Dev

Sum Log values from table using second table

From Dev

Count or Sum most sold product from MySQL

From Dev

Count or Sum most sold product from MySQL

From Dev

GROUP BY and LEFT JOIN with COUNT from same table

From Dev

Mysql SUM and GROUP BY from 3 tables

From Dev

MYSQL sum from two different tables group by

From Dev

SUM multiple Count and Group by

From Dev

Get sum of a group by count

From Dev

Linux group by, sum and count

From Dev

MySQL get COUNT and SUM on different GROUP BY in one SELECT

Related Related

HotTag

Archive