简体   繁体   中英

Get count of each id in SQL Server

I have the following tables

dbo.employee

id          name      position_id      phone
-----------------------------------------------   
1           Gosho         1            5623565
2           Pesho         2            095656
3           Misho         3            0984654

dbo.repairs

id          service_id  client_id   employee_id    dt      price
----------- ----------- ----------- ----------- ---------- ------
3           1           2           1           2003-05-03 4.54
4           2           1           2           2013-05-03 3.55
5           1           2           1           2003-05-03 4.54
6           2           1           2           2013-05-03 3.55
7           1           2           1           2003-05-03 4.54
8           2           1           2           2013-05-03 3.55
9           1           2           1           2003-05-03 4.54
10          2           1           2           2013-05-03 3.55

I want to view how many repairs there are for each employee.

The result that I am aiming for should be something like this:

employee_id  repairs_count
--------------------------
1             4
2             4

Here is my code that i came up with

SELECT 
    COUNT(r.employee_id)
FROM
    repairs r
LEFT JOIN 
    repairs e ON r.id = e.id
WHERE 
    r.id = e.id

Thanks in advance !

Group by employee_id . Aggregate functions like count() apply to each group

select employee_id, count(*) as repairs_count
from repairs
group by employee_id

If you only care about employees with repairs -- as in your sample results -- then you don't need a join :

select r.employee_id, count(*) as cnt
from dbo.repairs r
group by r.employee_id;

If you want all employees, even those with no repairs, then use a left join :

select e.id, count(r.employee_id) as cnt
from dbo.employees e left join
     dbo.repairs r
     on e.employee_id = r.employee_id
group by e.id;

using a JOIN to add more informations from the employee table

SELECT 
   employee_id = e.id,
   e.name, 
   repairs_count = COUNT(r.id)
FROM
   dbo.employee e
   LEFT JOIN
   dbo.repairs r ON e.id = r.employee_id
GROUP BY
   e.id, e.name
HAVING
   COUNT(r.id) > 0

There are several ways to solve this

group by

select e.id,
       e.name,
       e.position_id,
       e.phone,
       count(*) as repairs_count
from   employee e
  left outer join repairs r on e.id = r.employeeid
group by e.id, e.name, e.position_id, e.phone

NOTE: if you want to leave out employees that have no repairs, simply replace the left join by an inner join

or by subquery

select e.id,
       e.name,
       e.position_id,
       e.phone,
       (select count(1) from repairs r where r.employeeid = e.id) as repairs_count
from   employee e

I would go for the first option since the optimizer will have it easier to use indexes and its easier to leave out employees that have no repairs should you want that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM