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

[기초 통계량 - 그룹별 총합계(Total) 계산] 그룹별 합계 집계 - 44 (오라클 SQL, R, Python, SAS)

by 기서무나구물 2021. 8. 10.

포스팅 목차

     

    44. Display department numbers and total salary for each department.

     

    * 개별 부서에 근무하는 직원의 총 급여를 집계하여서 출력하시오.


     

    • 좀 더 상세한 내용은 42문제 와 아래 내용 참조 - 동일 문제
    • [데이터 관리] 8. By 또는 Split 파일 프로세싱 [링크]
    • Python stackoverflow : [링크]
    • R Package stackoverflow : [링크]
    • Oracle : group by, sum()
    • 파이썬 Pandas : groupby(), describe(), reset_index(), agg()
    • R 프로그래밍 : aggregate(), list, sum, function
    • R Dplyr Package : dplyr::group_by(), dplyr::summarise(), sum()
    • R sqldf Package : group by, sum()
    • Python pandasql Package : group by, sum()
    • R data.table Package a: sum(), list
    • SAS Proc SQL : group by, sum()
    • SAS Data Step : proc summary, sum=, first., last.
    • Python Dfply Package : group_by(), summarize(), .sum()
    • 파이썬 Base 프로그래밍 :

     


    1. Oracle(오라클)

     

    Group by와 sum() 함수

     

    Oracle Programming
    select deptno, 
           sum(sal) tot_sal
    from   emp 
    group by deptno;

     

     


    2. Python Pandas(파이썬)

     

    Groupby와 sum() 함수

     

    Python Programming
    emp[['sal']].groupby(emp['deptno']).sum().reset_index()

     

    Results
      deptno sal
    0 10 8750
    1 20 10875
    2 30 9400

     


    Group by와 집계함수(agg 함수) 를 통한 sum() 함수

     

    Python Programming
    emp.groupby(['deptno'])['sal'].agg('sum').reset_index(name='tot_sal')

     

    Results
      deptno tot_sal
    0 10 8750
    1 20 10875
    2 30 9400

     

     


    3. R Programming (R Package)

     

    by= 와 집계함수(aggregate 함수) 를 통한 sum() 함수

     

    R Programming
    %%R
    
    aggregate(list(tot_sal = emp$sal), by=list(deptno = emp$deptno), FUN=sum)

     

    Results
      deptno tot_sal
    1     10    8750
    2     20   10875
    3     30    9400

     


    집계함수(aggregate 함수) 와 sum 인수

     

    R Programming
    %%R
    
    aggregate(sal~deptno, FUN=sum, data=emp)

     

    Results
      deptno   sal
    1     10  8750
    2     20 10875
    3     30  9400

     

     


    4. R Dplyr Package

     

    Group_by와 집계함수(summarise 함수) 를 통한 sum() 연산

     

    R Programming
    %%R
    
    emp %>%
      dplyr::group_by(deptno) %>% 
      dplyr::summarise( tot_sal = sum(sal))

     

    Results
    `summarise()` ungrouping output (override with `.groups` argument)
    # A tibble: 3 x 2
      deptno tot_sal
       <dbl>   <dbl>
    1     10    8750
    2     20   10875
    3     30    9400

     

     


    5. R sqldf Package

     

    Group by와 sum() 함수

     

    R Programming
    %%R
    sqldf("select deptno, sum(sal) tot_sal 
           from emp 
           group by deptno;")

     

    Results
      deptno tot_sal
    1     10    8750
    2     20   10875
    3     30    9400

     

     


    6. Python pandasql Package

     

    Group by와 sum() 함수

     

    Python Programming
    ps.sqldf("select deptno, sum(sal) sal from emp group by deptno;")

     

    Results
      deptno sal
    0 10 8750
    1 20 10875
    2 30 9400

     

     


    7. R data.table Package

     

    by= 와 sum() 함수

     

    R Programming
    %%R
    
    DT <- data.table(emp)
    DT[, .(tot_sal = sum(sal)), by = list(deptno)][order(deptno),]

     

    Results
       deptno tot_sal
    1:     10    8750
    2:     20   10875
    3:     30    9400

     

     


    8. SAS Proc SQL

     

    Group by와 sum() 함수

     

    SAS Programming
    %%SAS sas
    
    PROC SQL;
      CREATE TABLE STATSAS_1 AS
        select deptno, sum(sal) as tot_sal
        from   emp 
        group 
           by  1;
    QUIT;
    PROC PRINT;RUN;

     

    Results
    OBS deptno tot_sal
    1 10 8750
    2 20 10875
    3 30 9400

     

     


    9. SAS Data Step

     

    Proc summary 프로시져의 sum= 인수

     

    SAS Programming
    %%SAS sas
    
    PROC SUMMARY DATA=EMP NWAY;
         CLASS deptno;
         VAR   SAL;
         OUTPUT OUT=EMP_SUM(DROP=_:) sum=tot_sal;
    RUN;
    PROC PRINT;RUN;

     

    Results
    OBS deptno tot_sal
    1 10 8750
    2 20 10875
    3 30 9400

     


    by 구문과 누적 합계

     

    SAS Programming
    %%SAS sas
    
    PROC SORT DATA=EMP OUT=EMP_1;
         BY DEPTNO;
    RUN;
    
    DATA EMP_SUM;
     SET EMP_1;
         BY DEPTNO;
    
         IF FIRST.DEPTNO THEN tot_sal = SAL;
         ELSE tot_sal + SAL;
    
         IF LAST.DEPTNO THEN OUTPUT;
         KEEP DEPTNO tot_sal;
    RUN;
    PROC PRINT;RUN;

     

    Results
    OBS deptno tot_sal
    1 10 8750
    2 20 10875
    3 30 9400

     

     


    10. Python Dfply Package

     

    Group_by와 summarize() 함수의 sum()

     

    Python Programming
    emp >> group_by('deptno') >> summarize( sal_tot = X.sal.sum() )

     

    Results
      deptno sal_tot
    0 10 8750
    1 20 10875
    2 30 9400

     


     

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

     

    반응형

    댓글