I have a table that looks like this:
我有一个看起来像这样的表:
posid sales eid
1 20 001
1 20 002
1 30 001
2 30 001
1 30 002
2 30 001
1 30 002
2 20 002
2 10 002
I want to write a query that would give me sum of sales for each employee on particular pos. the result needs to be like this.
我想写一个查询,它会给我特定pos上每个员工的销售总额。结果需要像这样。
pos id emp id sales
1 001 50
1 002 80
2 001 60
2 002 30
How would I do this?
我该怎么做?
2 个解决方案
#1
Use group by:
使用分组:
select t.posid
, t.eid
, sum(t.sales) as sales_by_posid
from mytable t
group by t.posid, t.eid
order by sales_by_posid desc
#2
SELECT
posID, empID, sum(sales)
FROM your_table
GROUP BY posID, empID
ORDER BY posID, empID
Here's a group by tutorial: http://www.tizag.com/mysqlTutorial/mysqlgroupby.php
这是一个教程组:http://www.tizag.com/mysqlTutorial/mysqlgroupby.php
#1
Use group by:
使用分组:
select t.posid
, t.eid
, sum(t.sales) as sales_by_posid
from mytable t
group by t.posid, t.eid
order by sales_by_posid desc
#2
SELECT
posID, empID, sum(sales)
FROM your_table
GROUP BY posID, empID
ORDER BY posID, empID
Here's a group by tutorial: http://www.tizag.com/mysqlTutorial/mysqlgroupby.php
这是一个教程组:http://www.tizag.com/mysqlTutorial/mysqlgroupby.php