• 카테고리

    질문 & 답변
  • 세부 분야

    데이터베이스

  • 해결 여부

    해결됨

Group by를 이용한 월별 입사동기를 구할 때 좀 더 깔끔한 방법 질문

24.01.14 00:55 작성 조회수 106

0

안녕하세요.

마지막에 group by로 묶어서 입사동기로 나올 때, 뒤에 일자 + 00:00:00 까지 나오는 게 보기가 안 좋아서요. 년-월만 나오게 할 수 있을까요? 가령,

select date_part('year',hire_month)||'-'||date_part('month', hire_month), counts

from

(

select date_trunc('month', hiredate) as hire_month, count(*) as counts

from hr.emp_test

group by date_trunc('month', hiredate)

);

 

로 하면 나오긴 하는데 이건 서브쿼리를 이용한 방법이라 좀 더 간결하게 표현하고 싶습니다.

답변 1

답변을 작성해보세요.

1

안녕하십니까,

이렇게 하면 될 것 같습니다.

select to_char(date_trunc('month', hiredate), 'yyyy-mm') as hire_month, count(*) as counts

from hr.emp

group by to_char(date_trunc('month', hiredate), 'yyyy-mm') order by 1;

 

감사합니다.