天天看點

選讀SQL經典執行個體筆記11_結果集變換

作者:躺着的柒
選讀SQL經典執行個體筆記11_結果集變換

1. 變換結果內建一行

1.1. 結果集

DEPTNO        CNT
------ ----------
    10          3
    20          5
    30          6           

1.2. 結果集

DEPTNO_10  DEPTNO_20  DEPTNO_30
--------- ---------- ----------
        3          5          6           

1.3. sql

select sum(case when deptno=10 then 1 else 0 end) as deptno_10,
        sum(case when deptno=20 then 1 else 0 end) as deptno_20,
        sum(case when deptno=30 then 1 else 0 end) as deptno_30
 from emp           

1.3.1. 對于每一行的原始資料,使用CASE表達式把行變換成列

1.4. sql

select max(case when deptno=10 then empcount else null end) as deptno_10,
       max(case when deptno=20 then empcount else null end) as deptno_20,
       max(case when deptno=30 then empcount else null end) as deptno_30
  from (
select deptno, count(*) as empcount
  from emp
 group by deptno
       ) x           

1.4.1. 用内嵌視圖生成每個部門的員工總人數

1.4.2. 主查詢裡的CASE表達式把行轉換成列

1.4.3. 調用MAX函數把幾列合并為一行

2. 反向變換結果集

2.1. 結果集

DEPTNO_10  DEPTNO_20  DEPTNO_30
--------- ---------- ----------
        3          5          6           

2.2. 結果集

DEPTNO        CNT
------ ----------
    10          3
    20          5
    30          6           

2.3. sql

select dept.deptno,
         case dept.deptno
              when 10 then emp_cnts.deptno_10
              when 20 then emp_cnts.deptno_20
              when 30 then emp_cnts.deptno_30
         end as CNT
    from (
  select sum(case when deptno=10 then 1 else 0 end) as deptno_10,
         sum(case when deptno=20 then 1 else 0 end) as deptno_20,
         sum(case when deptno=30 then 1 else 0 end) as deptno_30
    from emp
         ) emp_cnts,
         (select deptno from dept where deptno <= 30) dept           

3. 變換結果內建多行

3.1. 結果集

JOB       ENAME
--------- ----------
ANALYST   SCOTT
ANALYST   FORD
CLERK     SMITH
CLERK     ADAMS
CLERK     MILLER
CLERK     JAMES
MANAGER   JONES
MANAGER   CLARK
MANAGER   BLAKE
PRESIDENT KING
SALESMAN  ALLEN
SALESMAN  MARTIN
SALESMAN  TURNER
SALESMAN  WARD           

3.2. 結果集

CLERKS ANALYSTS MGRS  PREZ    SALES
------ -------- ----- ---- ------ ---------------
MILLER   FORD    CLARK      KING    TURNER
JAMES    SCOTT   BLAKE              MARTIN
ADAMS            JONES           WARD
SMITH                               ALLEN           

3.3. DB2

3.4. Oracle

3.5. SQL Server

3.6. 使用視窗函數ROW_NUMBER OVER確定每一個JOB/ENAME組合都是唯一的

select max(case when job='CLERK'
                  then ename else null end) as clerks,
         max(case when job='ANALYST'
                  then ename else null end) as analysts,
         max(case when job='MANAGER'
                  then ename else null end) as mgrs,
         max(case when job='PRESIDENT'
                  then ename else null end) as prez,
         max(case when job='SALESMAN'
                  then ename else null end) as sales
    from (
  select job,
         ename,
         row_number()over(partition by job order by ename) rn
    from emp
         ) x
   group by rn           

3.6.1.1. 為了剔除掉Null,需要調用聚合函數MAX,并基于RN執行GROUP BY

3.7. PostgreSQL

3.8. MySQL

3.9. sql

select max(case when job='CLERK'
                  then ename else null end) as clerks,
         max(case when job='ANALYST'
                  then ename else null end) as analysts,
         max(case when job='MANAGER'
                  then ename else null end) as mgrs,
         max(case when job='PRESIDENT'
                  then ename else null end) as prez,
         max(case when job='SALESMAN'
                  then ename else null end) as sales
    from (
  select e.job,
         e.ename,
         (select count(*) from emp d
           where e.job=d.job and e.empno < d.empno) as rnk
    from emp e
         ) x
   group by rnk           

3.9.1.1. 使用标量子查詢基于EMPNO為每個員工排序

3.9.1.2. 針對标量子查詢的傳回值執行GROUP BY

3.9.1.3. 使用CASE表達式和聚合函數MAX實作結果集變換

4. 反向變換結果內建一列

4.1. 把一個查詢結果合并成一列

4.1.1. 希望傳回DEPTNO等于10的全體員工的ENAME、JOB和SAL,并且想把3列值合并成1列

4.2. DB2

4.3. Oracle

4.4. SQL Server

4.5. 使用視窗函數ROW_NUMBER OVER

select case rn
              when 1 then ename
              when 2 then job
              when 3 then cast(sal as char(4))
         end emps
    from (
  select e.ename,e.job,e.sal,
         row_number()over(partition by e.empno
                              order by e.empno) rn
    from emp e,
         (select *
            from emp where job='CLERK') four_rows
   where e.deptno=10
         ) x           

5. 删除重複資料

5.1. 結果集

DEPTNO ENAME
------ ---------
    10 CLARK
       KING
       MILLER
    20 SMITH
       ADAMS
       FORD
       SCOTT
       JONES
    30 ALLEN
       BLAKE
       MARTIN
       JAMES
       TURNER
       WARD           

5.1.1. 每個DEPTNO隻顯示一次

5.2. DB2

5.3. SQL Server

5.4. 使用視窗函數MIN OVER

select case when empno=min_empno
              then deptno else null
         end deptno,
         ename
    from (
  select deptno,
         min(empno)over(partition by deptno) min_empno,
         empno,
         ename
    from emp
         ) x           

5.5. Oracle

select to_number(
           decode(lag(deptno)over(order by deptno),
                 deptno,null,deptno)
        ) deptno, ename
   from emp           

6. 變換結果集以實作跨行計算

select deptno, sum(sal) as sal
  from emp
 group by deptno
DEPTNO        SAL
------ ----------
    10       8750
    20      10875
    30       9400           

6.2. 算出上述DEPTNO 20和DEPTNO 10之間的工資總額的內插補點,以及上述DEPTNO 20和DEPTNO 30之間的工資總額內插補點

select d20_sal - d10_sal as d20_10_diff,
         d20_sal - d30_sal as d20_30_diff
    from (
  select sum(case when deptno=10 then sal end) as d10_sal,
         sum(case when deptno=20 then sal end) as d20_sal,
         sum(case when deptno=30 then sal end) as d30_sal
    from emp
         ) totals_by_dept