[ 서버 & DB 관련 ]/MySQL

다양한 조건에 따른 집계의 예제

BIZLAB 2019. 7. 18. 15:49
SELECT
  sum(if(trim(A.indiv) = '정상출근' or trim(A.indiv) = '지각', 1, 0)) as 'in',
  sum(if(trim(A.indiv) = '지각', 1, 0)) as 'late',
  sum(if(trim(A.instatus) = '휴가', 1, 0)) as 'holiday1',
  sum(if(trim(A.instatus) = '오전반차', 0.5, 0)) as 'holiday2',
  sum(if(trim(A.instatus) = '오후반차', 0.5, 0)) as 'holiday3',
  sum(if(trim(A.instatus) = '휴일근무', 1, 0)) as 'holiday_work',
  sum(if(trim(A.instatus) = '출장', 1, 0)) as 'out1',
  sum(if(trim(A.overtime_status) = '승인', 1, 0)) as 'out2',
  sum(if(trim(A.outwork_time) like '종일외근' and outwork_status='승인', 1, 0)) as 'out3',
  sum(if(trim(A.outwork_time) like '오전외근' and outwork_status='승인', 0.5, 0)) as 'out4',
  sum(if(trim(A.outwork_time) like '오후외근' and outwork_status='승인', 0.5, 0)) as 'out5'
from attendance as A join member as B
on A.id = B.id
where B.join <> '' 등의 적절한 조건