+ All Categories
Home > Documents > CSED421 Database Systems Lab Extended Group By Queries.

CSED421 Database Systems Lab Extended Group By Queries.

Date post: 17-Dec-2015
Category:
Upload: kelley-daniels
View: 220 times
Download: 3 times
Share this document with a friend
15
CSED421 Database Systems Lab Extended Group By Queries
Transcript
Page 1: CSED421 Database Systems Lab Extended Group By Queries.

CSED421Database Systems Lab

Extended Group By Queries

Page 2: CSED421 Database Systems Lab Extended Group By Queries.

Group function

GROUP BY Sort the data with distinct value for data of specified col-

umns Limit the window of data processed by the aggregate func-

tion

Find the job and average salary of each jobs Select job, avg(salary) from DevelopTeam group by job;

If you also want to determine the average salary over all jobs, you must run an additional query.

Page 3: CSED421 Database Systems Lab Extended Group By Queries.

Cause extra rows to be added to the summary output ROLLUP CUBE

Extended Group

Page 4: CSED421 Database Systems Lab Extended Group By Queries.

WITH ROLLUP Execute additional implicit GROUP BY queries with columns,

which is specified in GROUP BY clauses

Produce group summaries from right to left and a total sum-mary

SQL Example SELECT job, AVG(sal)

FROM EMPGROUP BY jobWITH ROLLUP;

SELECT AVG(sal)FROM EMP;

ROLLUP

Page 5: CSED421 Database Systems Lab Extended Group By Queries.

If “n” is the number of columns listed in the GROUP BY clause, there will be n+1 levels of summaries.

Group by Continent, Country, City with ROLLUP (Continent, Country, City) (Continent, Country) (Continent) ()

ROLLUP

Page 6: CSED421 Database Systems Lab Extended Group By Queries.

Results depends on a se-quence of columns in GROUP BY clauses.

ROLLUP for Multiple Col-umns

SELECT deptno, job, AVG(sal)FROM EMPGROUP BY deptno, jobWITH ROLLUP;

SELECT job, deptno, AVG(sal)FROM EMPGROUP BY job, deptnoWITH ROLLUP;

Page 7: CSED421 Database Systems Lab Extended Group By Queries.

Specify some conditions for ROLLUP

ROLLUP with Conditions

SELECT deptno, job, AVG(sal)FROM EMPGROUP BY deptno, jobWITH ROLLUPHAVING AVG(sal) > 2000;

Page 8: CSED421 Database Systems Lab Extended Group By Queries.

WITH CUBE Generate summaries for all combinations of the columns spec-

ified in GROUP BY clauses

If “n” is the number of columns listed in the GROUP BY clause, there will be summary combinations.

GROUP BY A, B, C with CUBE (A, B, C) (A, B), (A, C), (B, C) (A), (B), (C), (D) ()

CUBE

Page 9: CSED421 Database Systems Lab Extended Group By Queries.

MySQL doesn’t yet support CUBE.

CUBE

SELECT deptno, job, AVG(sal)FROM EMPGROUP BY deptno, jobWITH CUBE;

SELECT deptno, job, AVG(sal)FROM EMPGROUP BY deptno, jobWITH ROLLUPUNIONSELECT deptno, job, AVG(sal)FROM EMPGROUP BY job, deptno With ROLLUP;

Page 10: CSED421 Database Systems Lab Extended Group By Queries.

EMP table

DEPT table

Practice

EMPNO NUMBER(4) 직번

ENAME VARCHAR2(10) 이름

JOB VARCHAR2(9) 직위

MGR_EMPNO NUMBER(4) 상사 직번

HIREDATE DATE 고용일

SAL NUMBER(7, 2) 급여

COMM NUMBER(7, 2) 추가 급여

DEPTNO NUMBER(2) 부서번호

DEPTNO NUMBER(2) 부서번호

DNAME VARCHAR2(14) 부서명

LOC VARCHAR2(13) 부서위치

Page 11: CSED421 Database Systems Lab Extended Group By Queries.

Source lab6.sql

http://ids.postech.ac.kr/dblab2014/lab6.sql

Practice

Page 12: CSED421 Database Systems Lab Extended Group By Queries.

1. 부서명 (DNAME) 및 직위 (JOB) 별 급여 (SAL) 의 합을 다음과 같이 출력하시오 .

Practice

급여의 내림차순으로 정렬 MySQL 에서 ROLLUP 과

ORDER BY 는 동시에 사용할 수 없으므로 subquery 를 사용하여 해결

Page 13: CSED421 Database Systems Lab Extended Group By Queries.

2. 부서명 (DNAME) 및 직위 (JOB) 별 급여 (SAL) 의 합을 다음과 같이 출력하시오 .

Practice

급여의 내림차순으로 정렬 급여의 합이 2000 이상인 부서

및 직위 그룹을 출력 (Use cube)

Page 14: CSED421 Database Systems Lab Extended Group By Queries.

3. 관리자가 있는 사람들에 대해서 각 부서 (DEPTNO) 별 직위 (JOB)별로 직원수를 다음과 같은 형태로 출력하시오 .

Practice

부서 (DEPTNO), 직위 (JOB) 순으로 정렬

Page 15: CSED421 Database Systems Lab Extended Group By Queries.

4. 관리자의 이름과 관리자가 관리하는 사원들의 직위별 급여 합 , 직원 수 , 평균 급여를 다음과 같이 출력하시오 .

Practice

관리자의 이름은 중복이 없다고 가정


Recommended