본문 바로가기
통계프로그램 비교 시리즈/프로그래밍비교(Oracle,Python,R,SAS)

[일자 추출] 특정 날짜에 해당하는 데이터 추출 - 133 (오라클 SQL, R, Python, SAS)

by 기서무나구물 2022. 12. 18.

포스팅 목차

     

    133. Display those employees who joined the company before 15th of the month?

     

    * 입사일자가 매월 15일 이전에 입사한 직원들의 정보를 출력하시오.


    • Oracle : To_char()
    • 파이썬 Pandas : datetime.now(), timestamp.strftime(), pd.to_numeric(), .str.slice()
    • R 프로그래밍 : as.numeric(), base::substr()
    • R Dplyr Package : as.numeric(), stringr::str_sub()
    • R sqldf Package : typeof(), cast(), substr(), datetime()
    • Python pandasql Package : typeof(), cast(), substr(), datetime(), strftime()
    • R data.table Package : as.numeric(), stringr::str_sub()
    • SAS Proc SQL : INPUT(), PUT(), DAY()
    • SAS Data Step : INPUT(), PUT(), DAY() 함수
    • Python Dfply Package : make_symbolic(), pd.to_numeric(), str.slice(), filter_by()
    • 파이썬 Base 프로그래밍 :

     


    1. Oracle(오라클)

    입사일자에서 일자에 해당하는 부분을 추출하여서 15일 이전에 입사한 직원 정보를 출력한다.

     

    Oracle Programming
    select ename 
    from   emp 
    where  to_char(hiredate,'DD') < 15;

     


    2. Python Pandas(파이썬)

    • [참고] 현재 일자 출력
    Python Programming
    from datetime import datetime
    
    timestamp=datetime.now()
    print("현재 Timestamp :",timestamp)
    
    # 현재 timestamp (yyyy-mm-dd, HH:MM:SS)
    cur_day_format = timestamp.strftime("%Y-%m-%d,%H:%M:%S ")
    print("현재 Timestamp (YYYY-MM-DD, HH:MM:SS):", cur_day_format)
    
    # 현재일자 (yyyymmdd format)
    cur_day_format = timestamp.strftime("%Y%m%d")
    print("현재 Date(YYYYMMDD):", cur_day_format)
    
    # 현재일자 (dd-mm-yyyy format)
    cur_day_format=timestamp.strftime("%d-%m-%Y")
    print("현재 Date(DD-MM-YYYY):",cur_day_format)
    
    # 현재일자 ( dd/mm/yyyy format )
    cur_day_format=timestamp.strftime("%d/%m/%Y")
    print("현재 Date(DD-MM-YYYY):",cur_day_format)
    
    # 현재일자 ( textual month day and Year(Month dd, YYYY) )
    cur_day_format = timestamp.strftime("%B %d, %Y")
    print("현재 Date(Month DD,YYYY):", cur_day_format)
    
    # 현재일자 ( textual day and DD textual Month , YYYY )
    cur_day_format =timestamp.strftime("%A %d %B, %Y")
    print("현재 Date(Day  DD Month, YYYY):", cur_day_format) 

     

    Results
    현재 Timestamp : 2021-03-28 00:59:55.062120
    현재 Timestamp (YYYY-MM-DD, HH:MM:SS): 2021-03-28,00:59:55 
    현재 Date(YYYYMMDD): 20210328
    현재 Date(DD-MM-YYYY): 28-03-2021
    현재 Date(DD-MM-YYYY): 28/03/2021
    현재 Date(Month DD,YYYY): March 28, 2021
    현재 Date(Day  DD Month, YYYY): Sunday 28 March, 2021

     


    Series.str.slice() 함수를 사용하여 입사일자(‘hiredate’)에서 일자 부분을 추출한 후 15일 이전에 입사한 직원의 정보를 출력한다.

     

    Python Programming
    emp [ pd.to_numeric(emp['hiredate'].str.slice(8,10)) < 15 ]

     

    Results
      empno ename job mgr hiredate sal comm deptno
    3 7566 JONES MANAGER 7839.0 1981/04/02 2975 NaN 20
    5 7698 BLAKE MANAGER 7839.0 1981/03/01 2850 NaN 30
    6 7782 CLARK MANAGER 7839.0 1981/01/09 2450 NaN 10
    7 7788 SCOTT ANALYST 7566.0 1982/12/09 3000 NaN 20
    9 7844 TURNER SALESMAN 7698.0 1981/09/08 1500 0.0 30
    10 7876 ADAMS CLERK 7788.0 1983/01/12 1100 NaN 20
    11 7900 JAMES CLERK 7698.0 1981/12/03 950 NaN 30
    12 7902 FORD ANALYST 7566.0 1981/12/03 3000 NaN 20

     


    3. R Programming (R Package)

    base::substr() 함수를 사용하여 입사일자(‘hiredate’)에서 일자 부분을 추출한 후 15일 이전에 입사한 직원의 정보를 출력한다.

     

    R Programming
    %%R
    
    emp[ ( as.numeric(base::substr(emp$hiredate,9,10)) ) < 15 , ]

     

    Results
    # A tibble: 8 x 8
      empno ename  job        mgr hiredate     sal  comm deptno
      <dbl> <chr>  <chr>    <dbl> <date>     <dbl> <dbl>  <dbl>
    1  7566 JONES  MANAGER   7839 1981-04-02  2975    NA     20
    2  7698 BLAKE  MANAGER   7839 1981-03-01  2850    NA     30
    3  7782 CLARK  MANAGER   7839 1981-01-09  2450    NA     10
    4  7788 SCOTT  ANALYST   7566 1982-12-09  3000    NA     20
    5  7844 TURNER SALESMAN  7698 1981-09-08  1500     0     30
    6  7876 ADAMS  CLERK     7788 1983-01-12  1100    NA     20
    7  7900 JAMES  CLERK     7698 1981-12-03   950    NA     30
    8  7902 FORD   ANALYST   7566 1981-12-03  3000    NA     20

     


    4. R Dplyr Package

    stringr::str_sub 함수를 사용하여 입사일자(‘hiredate’)에서 일자 부분을 추출한 후 dplyr::filter() 함수를 사용하여서 15일 이전에 입사한 직원의 정보를 출력한다.

     

    R Programming
    %%R
    
    library(stringi)
    
    emp %>% 
      dplyr::filter( as.numeric(stringr::str_sub(hiredate,9,10)) < 15 )

     

    Results
    # A tibble: 8 x 8
      empno ename  job        mgr hiredate     sal  comm deptno
      <dbl> <chr>  <chr>    <dbl> <date>     <dbl> <dbl>  <dbl>
    1  7566 JONES  MANAGER   7839 1981-04-02  2975    NA     20
    2  7698 BLAKE  MANAGER   7839 1981-03-01  2850    NA     30
    3  7782 CLARK  MANAGER   7839 1981-01-09  2450    NA     10
    4  7788 SCOTT  ANALYST   7566 1982-12-09  3000    NA     20
    5  7844 TURNER SALESMAN  7698 1981-09-08  1500     0     30
    6  7876 ADAMS  CLERK     7788 1983-01-12  1100    NA     20
    7  7900 JAMES  CLERK     7698 1981-12-03   950    NA     30
    8  7902 FORD   ANALYST   7566 1981-12-03  3000    NA     20

     


    5. R sqldf Package

    입사일자에서 일자에 해당하는 부분을 추출하여서 15일 이전에 입사한 직원 정보를 출력한다.

    • 현재 hiredate가 “real” 형태로 저장되어 있어서 datetime 함수를 사용하여서 문자형을 전환 후 substr() 함수를 사용하여서 일자를 추출하고, cast 함수로 문자를 정수로 변환 후 15일 이전에 입사한 직원들을 선택한다.
    • to_char(hiredate,’DD’) : 77 / 126 / 129 / 130 / 133
    R Programming
    %%R
    
    sqldf(" select empno,ename,
                   typeof(hiredate) hire_type,
                   cast(substr(datetime(hiredate * 3600 * 24,'unixepoch'),9,2) as int) hire_day
            from   emp 
            where  cast(substr(datetime(hiredate * 3600 * 24,'unixepoch'),9,2) as int) < 15 ")

     

    Results
      empno  ename hire_type hire_day
    1  7566  JONES      real        2
    2  7698  BLAKE      real        1
    3  7782  CLARK      real        9
    4  7788  SCOTT      real        9
    5  7844 TURNER      real        8
    6  7876  ADAMS      real       12
    7  7900  JAMES      real        3
    8  7902   FORD      real        3

     


    6. Python pandasql Package

    strftime 함수에 %d format을 지정하여서 입사일자(‘hiredate’)에서 일자 부분을 추출하고, cast 함수로 문자를 정수로 변환 후 15일 이전에 입사한 직원들을 선택한다.

     

    Python Programming
    ps.sqldf(" select empno,ename,                                                                                                           \
                      strftime('%Y%m%d',(substr(hiredate,1,4)||'-'||substr(hiredate,6,2)||'-'||substr(hiredate,9,2)))          hire_date,    \
                      typeof(strftime('%Y%m%d',(substr(hiredate,1,4)||'-'||substr(hiredate,6,2)||'-'||substr(hiredate,9,2))))  hire_type,    \
                      strftime('%d',(substr(hiredate,1,4)||'-'||substr(hiredate,6,2)||'-'||substr(hiredate,9,2)))              hire_day,     \
                      cast(strftime('%d',(substr(hiredate,1,4)||'-'||substr(hiredate,6,2)||'-'||substr(hiredate,9,2))) as int) hire_day_int  \
               from emp \
               where cast(strftime('%d',(substr(hiredate,1,4)||'-'||substr(hiredate,6,2)||'-'||substr(hiredate,9,2))) as int) < 15;")

     

    Results
      empno ename hire_date hire_type hire_day hire_day_int
    0 7566 JONES 19810402 text 02 2
    1 7698 BLAKE 19810301 text 01 1
    2 7782 CLARK 19810109 text 09 9
    3 7788 SCOTT 19821209 text 09 9
    4 7844 TURNER 19810908 text 08 8
    5 7876 ADAMS 19830112 text 12 12
    6 7900 JAMES 19811203 text 03 3
    7 7902 FORD 19811203 text 03 3

     


    7. R data.table Package

    stringr::str_sub 함수를 사용하여 입사일자(‘hiredate’)에서 일자 부분을 추출한 후 15일 이전에 입사한 직원들의 정보를 출력한다.

     

    R Programming
    %%R
    
    DT          <- data.table(emp)
    dept_DT     <- data.table(dept)
    salgrade_DT <- data.table(salgrade)
    
    DT[ as.numeric(stringr::str_sub(hiredate,9,10)) < 15 , ]

     

    Results
       empno  ename      job  mgr   hiredate  sal comm deptno
    1:  7566  JONES  MANAGER 7839 1981-04-02 2975   NA     20
    2:  7698  BLAKE  MANAGER 7839 1981-03-01 2850   NA     30
    3:  7782  CLARK  MANAGER 7839 1981-01-09 2450   NA     10
    4:  7788  SCOTT  ANALYST 7566 1982-12-09 3000   NA     20
    5:  7844 TURNER SALESMAN 7698 1981-09-08 1500    0     30
    6:  7876  ADAMS    CLERK 7788 1983-01-12 1100   NA     20
    7:  7900  JAMES    CLERK 7698 1981-12-03  950   NA     30
    8:  7902   FORD  ANALYST 7566 1981-12-03 3000   NA     20

     


    8. SAS Proc SQL

    입사일자에서 일자에 해당하는 부분을 추출하여서 15일 이전에 입사한 직원 정보를 출력한다.

     

    SAS Programming
    %%SAS sas
    
    PROC SQL;
      CREATE TABLE STATSAS_1 AS
        select A.*,
               INPUT(PUT(hiredate,DAY.),8.) AS DAY_NUM,
               DAY(hiredate) AS DAY_NUM2
        from   emp A 
        WHERE  DAY(hiredate) < 15;
    QUIT;
    PROC PRINT Data=STATSAS_1(Drop=job sal comm);RUN;

     

    Results
    OBS empno ename mgr hiredate deptno DAY_NUM DAY_NUM2
    1 7566 JONES 7839 1981-04-02 20 2 2
    2 7698 BLAKE 7839 1981-03-01 30 1 1
    3 7782 CLARK 7839 1981-01-09 10 9 9
    4 7788 SCOTT 7566 1982-12-09 20 9 9
    5 7844 TURNER 7698 1981-09-08 30 8 8
    6 7876 ADAMS 7788 1983-01-12 20 12 12
    7 7900 JAMES 7698 1981-12-03 30 3 3
    8 7902 FORD 7566 1981-12-03 20 3 3

     


    9. SAS Data Step

     

    SAS Programming
    %%SAS sas
    
    DATA STATSAS_2; 
     SET EMP;
         DAY_NUM_1 = INPUT(PUT(hiredate,DAY.),8.);
         DAY_NUM_2 = DAY(hiredate);
         WHERE  DAY(hiredate) < 15;
    RUN;
    PROC PRINT Data=STATSAS_1(Drop=job sal comm);RUN;

     

    Results
    OBS empno ename mgr hiredate deptno DAY_NUM DAY_NUM2
    1 7566 JONES 7839 1981-04-02 20 2 2
    2 7698 BLAKE 7839 1981-03-01 30 1 1
    3 7782 CLARK 7839 1981-01-09 10 9 9
    4 7788 SCOTT 7566 1982-12-09 20 9 9
    5 7844 TURNER 7698 1981-09-08 30 8 8
    6 7876 ADAMS 7788 1983-01-12 20 12 12
    7 7900 JAMES 7698 1981-12-03 30 3 3
    8 7902 FORD 7566 1981-12-03 20 3 3

     


    10. Python Dfply Package

     

    Python Programming
    emp >> \
      filter_by ( make_symbolic(pd.to_numeric)(X.hiredate.str.slice(8,10)) < 15 )

     

    Results
      empno ename job mgr hiredate sal comm deptno
    3 7566 JONES MANAGER 7839.0 1981/04/02 2975 NaN 20
    5 7698 BLAKE MANAGER 7839.0 1981/03/01 2850 NaN 30
    6 7782 CLARK MANAGER 7839.0 1981/01/09 2450 NaN 10
    7 7788 SCOTT ANALYST 7566.0 1982/12/09 3000 NaN 20
    9 7844 TURNER SALESMAN 7698.0 1981/09/08 1500 0.0 30
    10 7876 ADAMS CLERK 7788.0 1983/01/12 1100 NaN 20
    11 7900 JAMES CLERK 7698.0 1981/12/03 950 NaN 30
    12 7902 FORD ANALYST 7566.0 1981/12/03 3000 NaN 20

     


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

    반응형

    댓글