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

Time:12-19

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;

Thanks in advance.

CodePudding user response:

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;

CodePudding user response:

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;

  • Related