tags:

views:

68

answers:

2

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 id    emp id     sales

  1       001        50
  1       002        80
  2       001        60
  2       002        30

How would I do this?

+2  A: 

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
Adam Bernier
A: 
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

Juliet