简体   繁体   中英

One query with combining data from two tables and grouping (sql, oracle)

I am trying to write which create a view that shows the number of departments for each location (Loc) and the number of employees working in the departments in one query.

Tables are provided below.

Table Dept

DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 RISK BOSTON

Table Emp

 EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
  9901 Kowalski   SALESMAN        9345 23-JAN-90        300        100         12
  7369 SMITH      CLERK           7902 17-MAR-80   15355.58                    20
  7499 ALLEN      SALESMAN        7698 20-MAR-81       1600        300         30
  7521 WARD       SALESMAN        7698 22-MAR-81       1250        500         30
  7566 JONES      MANAGER         7839 02-MAR-81   57103.26                    20
  7654 MARTIN     SALESMAN        7698 28-MAR-81       1250       1400         30
  7698 BLAKE      MANAGER         7839 01-MAR-81       2850                    30
  7782 CLARK      MANAGER         7839 09-MAR-81       2450                    10
  7788 SCOTT      ANALYST         7566 09-MAR-82   57583.15                    20
  7839 KING       PRESIDENT            17-MAR-81       5000                    10
  7844 TURNER     SALESMAN        7698 08-MAR-81       1500          0         30
  7876 ADAMS      CLERK           7788 12-MAR-83   21113.82                    20
  7900 JAMES      CLERK           7698 03-MAR-81        950                    30
  7902 FORD       ANALYST         7566 03-MAR-81   57583.15                    20
  7934 MILLER     CLERK           7782 23-MAR-82       1300                    10

The output should be

LOC               NO_DEP     NO_EMP
------------- ---------- ----------
NEW YORK               1         3
DALLAS                 1         1
CHICAGO                1         5
BOSTON                 2         6 

So far I was able to create two separate queries which are provided below and which gives this result however I must do it in a one query which will give output in one table.

SELECT DISTINCT d.Loc, Count(d.Deptno) AS No_Dep, d.Deptno
FROM Dept d
GROUP BY d.Loc, d.Deptno;

SELECT DISTINCT COUNT(e.Empno) OVER (PARTITION BY e.Deptno) AS No_Emp, e.Deptno
FROM Emp e;

Correct solution is

SELECT d.LOC, COUNT(DISTINCT d.DEPTNO) AS NO_DEP, COUNT(e.EMPNO) AS   NO_EMP  
FROM DEPT d  
LEFT JOIN EMP e ON d.DEPTNO = e.DEPTNO  
GROUP BY D.LOC;

You first need to join them and then and then need to do the aggregation -

SELECT D.LOC, COUNT(DISTINCT DEPTNO) NO_DEP, COUNT(EMPNO) NO_EMP
  FROM DEPT D
  JOIN EMP E ON D.DEPTNO = E.DEPTNO
 GROUP BY D.LOC;

you can use "group by" and "having" to solve such queries

The HAVING clause is used instead of WHERE with aggregate functions. While the GROUP BY Clause groups rows that have the same values into summary rows. The having clause is used with the where clause in order to find rows with certain conditions. The having clause is always used after the group By clause.

example:

select D.LOC AS LOC, COUNT(D.DEPTNO) AS NO_DEP, COUNT(distinct E.EMPNO) AS NO_EMP
from Dept D, Emp E
where D.DEPTNO=E.DEPTNO
group by D.LOC;

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