Query to count different (MSSQL Server 2005 Express)

2

I have the following table:

I need a number of different offices in each department:

I made the following query, but it is not what the statement asks:

SELECT depto_no, count(*)
FROM empleado
GROUP BY depto_no

The issue is that I already tried to put DISTINCT in SELECT , I also tried to put a subquery in FROM (I do not know if it's okay to do it that way).

The result should be as follows:

depto_no    valor
10          1
20          1
30          2 (los dos oficios distintos)
40          1
    
asked by MaxYbr 20.11.2017 в 15:12
source

1 answer

1
  

I need a number of different offices in each department

You have to put the DISTINCT within COUNT() :

SELECT depto_no, count(DISTINCT oficio) 
  FROM empleado 
 GROUP BY depto_no
    
answered by 20.11.2017 / 15:39
source