sql - How to Select Count for every user within a table -


i have table named "calls". table's columns organized as

callcounter, assignedempid, completedbyempid 1            200            200 2            200            200 3            201            200 4            200            200 5            201            201 6            201            200 7            200            200 8            200            200 9            200            201 10           201            201 ... 

how create select sql query return following data. ideally complete entire sql query in 1 query.

employee    # calls assigned    # calls completed 200         6                   7 201         4                   3 

i have tried following queries

select assignedempid, count(callcounter) calls select count(*) calls 

first need define list of employees. can subquery , union if don't have employees table join with.

then use conditional aggregation:

select t.empid,      sum(case when t.empid = c.assignedempid 1 else 0 end) assignedcount,     sum(case when t.empid = c.completedbyempid 1 else 0 end) completedcount (select distinct assignedempid empid calls        union select distinct completedbyempid calls) t         join calls c on t.empid in (c.assignedempid, c.completedbyempid) group t.empid 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -