현재 조회해야 하는 정보는 이런 형식입니다.
구분 | 전체 인원 | 지난주 접속직원 | 이번주 접속직원 |
a팀 | 50 | 10 | 30 |
b팀 | 40 | 30 | 20 |
c팀 | 10 | 4 | 4 |
d팀 | 100 | 11 | 34 |
합계 | 200 | 55 | 88 |
하지만 제가 쿼리로 만든 데이터는
구분 | 지난주 접속직원 |
a팀 | |
b팀 | |
c팀 | |
d팀 | |
합계 |
구분 | 전체인원 |
a팀 | |
b팀 | |
c팀 | |
d팀 | |
합계 |
구분 | 이번주 접속직원 |
a팀 | |
b팀 | |
c팀 | |
d팀 | |
합계 |
이렇게 3가지 select결과입니다 ㅠㅠ
참고로 금주, 지난주 접속 인원은 아래 쿼리로 처리하였습니다.
혹시 합치는 방법이나 다른좋은 방식이 있을까요?
select A.STAFF_TEAM_CD , COUNT(A.STAFF_TEAM_CD) from ( select B.STAFF_ID as STAFF_ID , MAX(B.REGIST_DT) as REGIST_DT FROM DB_CONN_LOG B WHERE date_trunc('week' , B.REGIST_DT) = date_trunc('week', now()) group by STAFF_ID) as B inner join DB_STAFF A on 1=1 and B.STAFF_ID = A.STAFF_ID and date_trunc('week' , B.REGIST_DT) = date_trunc('week', now()) group by A.STAFF_TEAM_CD;
MySQL 인 줄 알고 댓글 달았네요. -- MySQL -- SELECT a.staff_team_cd , COUNT(DISTINCT a.staff_id) cnt_user , COUNT(DISTINCT CASE WHEN b.regist_dt < date_trunc('week', NOW()) THEN b.staff_id END) cnt_1 , COUNT(DISTINCT CASE WHEN b.regist_dt >= date_trunc('week', NOW()) THEN b.staff_id END) cnt_2 FROM db_staff a LEFT OUTER JOIN db_conn_log b ON a.staff_id = b.staff_id AND b.regist_dt >= date_trunc('week', NOW() - INTERVAL 7 DAY) GROUP BY a.staff_team_cd WITH ROLLUP ;
-- MySQL -- SELECT a.staff_team_cd , COUNT(*) cnt_user , COUNT(x) cnt_1 , COUNT(y) cnt_2 FROM db_staff a LEFT OUTER JOIN (SELECT staff_id , MAX(CASE WHEN regist_dt < date_trunc('week', NOW()) THEN 1 END) x , MAX(CASE WHEN regist_dt >= date_trunc('week', NOW()) THEN 1 END) y FROM db_conn_log WHERE regist_dt >= date_trunc('week', NOW() - INTERVAL 7 DAY) GROUP BY staff_id ) b ON a.staff_id = b.staff_id GROUP BY a.staff_team_cd WITH ROLLUP ;
MySQL 인 줄 알고 댓글 달았네요.
-- PostgreSQL -- SELECT a.staff_team_cd , COUNT(*) cnt_user , COUNT(x) cnt_last_week , COUNT(y) cnt_this_week FROM db_staff a LEFT OUTER JOIN (SELECT staff_id , MAX(CASE WHEN regist_dt < DATE_TRUNC('week', NOW()) THEN 1 END) x , MAX(CASE WHEN regist_dt >= DATE_TRUNC('week', NOW()) THEN 1 END) y FROM db_conn_log WHERE regist_dt >= DATE_TRUNC('week', NOW() - INTERVAL '7 DAY') GROUP BY staff_id ) b ON a.staff_id = b.staff_id GROUP BY ROLLUP(a.staff_team_cd) ;