본문 바로가기
통계프로그램 비교 시리즈/오라클함수 비교(R & Python)

GREATEST 오라클 함수 [Oracle, Pandas, R Prog, Dplyr, Sqldf, Pandasql, Data.Table, DuckDB]

by 기서무나구물 2021. 11. 24.

포스팅 목차

    * 파이썬 & R 패키지 호출 및 예제 데이터 생성 링크


    [ GREATEST Oracle Function ]

     


    GREATEST함수는 하나 이상의 인수중에서 가장 큰 값을 반환한다.

     

     


    1. Oracle(오라클)

     

    Oracle Programming
    SELECT GREATEST ('HARRY', 'HARRIOT', 'HAROLD') "Greatest"
    FROM   DUAL;

     

    Results
    Greatest
    --------------
    HARRY

     

     


    2. Python Pandas(파이썬)

     

    Python Programming
    max('HARRY', 'HARRIOT', 'HAROLD')

     

     

    Results
    'HARRY'

     

     

     


    3. R Programming (R Package)

     

    R Programming
    %%R
    
    max('HARRY', 'HARRIOT', 'HAROLD')

     

     

    Results
    [1] "HARRY"

     

     

     


    4. R Dplyr Package

     

    R Programming
    %%R
    
    emp %>%
      rowwise() %>%
      dplyr::mutate(Greatest = max(ename, job)) %>%
      head(10)

     

     

    Results
    # A tibble: 10 x 9
    # Rowwise: 
       empno ename  job         mgr hiredate     sal  comm deptno Greatest 
       <dbl> <chr>  <chr>     <dbl> <date>     <dbl> <dbl>  <dbl> <chr>    
     1  7369 SMITH  CLERK      7902 1980-12-17   800    NA     20 SMITH    
     2  7499 ALLEN  SALESMAN   7698 1981-02-20  1600   300     30 SALESMAN 
     3  7521 WARD   SALESMAN   7698 1981-02-22  1250   500     30 WARD     
     4  7566 JONES  MANAGER    7839 1981-04-02  2975    NA     20 MANAGER  
     5  7654 MARTIN SALESMAN   7698 1981-09-28  1250  1400     30 SALESMAN 
     6  7698 BLAKE  MANAGER    7839 1981-03-01  2850    NA     30 MANAGER  
     7  7782 CLARK  MANAGER    7839 1981-01-09  2450    NA     10 MANAGER  
     8  7788 SCOTT  ANALYST    7566 1982-12-09  3000    NA     20 SCOTT    
     9  7839 KING   PRESIDENT    NA 1981-11-17  5000    NA     10 PRESIDENT
    10  7844 TURNER SALESMAN   7698 1981-09-08  1500     0     30 TURNER   

     

     

     


    5. R sqldf Package

     

    R Programming
    %%R
    
    sqldf(" SELECT max('HARRY', 'HARRIOT', 'HAROLD') Greatest   ")

     

     

    Results
      Greatest
    1    HARRY

     

     

     


    6. Python pandasql Package

     

    Python Programming
    ps.sqldf("  SELECT max('HARRY', 'HARRIOT', 'HAROLD') Greatest ")

     

     

    Results
    	Greatest
    0	HARRY

     

     

     


    7. R data.table Package

     

    행 단위 연산을 위하여 그룹 변수에 Seq를 지정

     

    R Programming
    %%R
    
    DT          <- data.table(emp)
    dept_DT     <- data.table(dept)
    
    DT[, Greatest := max(ename, job), by=1:nrow(DT)][1:7, ]

     

     

    Results
       empno  ename      job  mgr   hiredate  sal comm deptno Greatest
    1:  7369  SMITH    CLERK 7902 1980-12-17  800   NA     20    SMITH
    2:  7499  ALLEN SALESMAN 7698 1981-02-20 1600  300     30 SALESMAN
    3:  7521   WARD SALESMAN 7698 1981-02-22 1250  500     30     WARD
    4:  7566  JONES  MANAGER 7839 1981-04-02 2975   NA     20  MANAGER
    5:  7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400     30 SALESMAN
    6:  7698  BLAKE  MANAGER 7839 1981-03-01 2850   NA     30  MANAGER
    7:  7782  CLARK  MANAGER 7839 1981-01-09 2450   NA     10  MANAGER

     


    8. Python DuckDB의 SQL

     

    Python Programming
    %%sql
      SELECT GREATEST ('HARRY', 'HARRIOT', 'HAROLD') as "Greatest"

     

    Python Programming
    duckdb.sql(" SELECT GREATEST ('HARRY', 'HARRIOT', 'HAROLD') as Greatest_1 ").df()

     

     

    Results
      Greatest_1
    0      HARRY

     

     


    Transreal Fiction, Edinburgh, United Kingdom (https://unsplash.com/photos/O0czDp42LDA)

      --------------------------------------------  

     

    [Oracle, Pandas, R Prog, Dplyr, Sqldf, Pandasql, Data.Table] 오라클 함수와 R & Python 비교 사전 목록? 링크

     

    오라클 SQL 함수(Oracle SQL Function) 목록 리스트 링크

     

    [SQL, Pandas, R Prog, Dplyr, SQLDF, PANDASQL, DATA.TABLE] SQL EMP 예제로 만나는 테이블 데이터 처리 방법 리스트 링크 링크
    반응형

    댓글