설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


Powered by EnterpriseDB
총 게시물 89건, 최근 0 건
   

ORDER BY 절의 NULLS FIRST 과 NULL LAST

글쓴이 : PostgresDBA 날짜 : 2012-12-04 (화) 14:18 조회 : 4021
오라클에는 NULLS FIRST, NULLS LAST 구문이 있습니다.

PostgreSQL 에도 위 구문을 그대로 사용할수 있습니다.
아래 결과를 살펴 볼까요?

scott@[local]:5432 scottdb#SQL> select * from emp order by comm;  -- 널아닌 값이 먼저 보입니다.
 empno | ename  |    job    | mgr  |  hiredate  | sal  | comm | deptno 
-------+--------+-----------+------+------------+------+------+--------
  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30
  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30
  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30
  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30
  7788 | SCOTT  | ANALYST   | 7566 | 1982-12-09 | 3000 | NULL |     20
  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10
  7876 | ADAMS  | CLERK     | 7788 | 1983-01-12 | 1100 | NULL |     20
  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30
  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20
  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20
  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300 | NULL |     10
  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20
  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30
  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10
(14 rows)

Time: 2.544 ms

-- NULLS LAST 를 안붙였을때와 결과는 동일합니다.
scott@[local]:5432 scottdb#SQL> select * from emp order by comm nulls last;
 empno | ename  |    job    | mgr  |  hiredate  | sal  | comm | deptno 
-------+--------+-----------+------+------------+------+------+--------
  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30
  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30
  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30
  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30
  7788 | SCOTT  | ANALYST   | 7566 | 1982-12-09 | 3000 | NULL |     20
  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10
  7876 | ADAMS  | CLERK     | 7788 | 1983-01-12 | 1100 | NULL |     20
  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30
  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20
  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20
  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300 | NULL |     10
  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20
  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30
  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10
(14 rows)

Time: 0.660 ms

-- 널값이 먼저 보이네요
scott@[local]:5432 scottdb#SQL> select * from emp order by comm nulls first;
 empno | ename  |    job    | mgr  |  hiredate  | sal  | comm | deptno 
-------+--------+-----------+------+------------+------+------+--------
  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300 | NULL |     10
  7876 | ADAMS  | CLERK     | 7788 | 1983-01-12 | 1100 | NULL |     20
  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30
  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20
  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20
  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20
  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30
  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10
  7788 | SCOTT  | ANALYST   | 7566 | 1982-12-09 | 3000 | NULL |     20
  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10
  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30
  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30
  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30
  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30
(14 rows)

Time: 0.728 ms
scott@[local]:5432 scottdb#SQL> 

   

postgresdba.com