希望这可以帮助你:
insert into t_department values( 1, 'MANAGER');
insert into t_department values( 2, 'OTHERS');
insert into t_employee values( 1, 1, NULL ,'NAME-1', 50000, sysdate, 'ADDRESS','Y');
insert into t_employee VALUES( 2, 2, NULL, 'NAME-2', 40000, SYSDATE, 'ADDRESS','Y');
insert into t_employee values( 3, 2, null, 'NAME-3', 50000, sysdate-200, 'ADDRESS','Y');
insert into t_employee values( 4, 2, null, 'NAME-4', 40000, sysdate-100, 'ADDRESS','Y');
insert into t_employee values( 5, 2, null, 'NAME-5', 40000, sysdate-100, 'ADDRESS','Y');
insert into t_employee values( 6, 2, null, 'NAME-6', 40000, sysdate-100, 'ADDRESS',NULL);
select to_char(birth_date, 'mon') "month",
LISTAGG(e.name||'('||NVL(e.status,'Not Defined')||')', ',') WITHIN GROUP (
ORDER BY TO_CHAR(BIRTH_DATE, 'mon')) AS lists
FROM t_employee e
JOIN t_department d
ON( e.department_id = d.id)
where d.name <> 'MANAGER'
GROUP BY TO_CHAR(BIRTH_DATE, 'mon');
MONTH LISTS
------------ ------------------------------------------
apr NAME-3(Y)
jul NAME-4(Y),NAME-5(Y),NAME-6(Not Defined)
oct NAME-2(Y)
或者简单地说:
select to_char(birth_date, 'mon') month, nvl(e.status,'Not Defined')status,
listagg(e.name, ',')
WITHIN GROUP (ORDER BY TO_CHAR(BIRTH_DATE, 'mon')) AS lists
FROM t_employee e
JOIN t_department d
ON( e.department_id = d.id)
where d.name <> 'MANAGER'
group by to_char(birth_date, 'mon'),e.status;
MONTH STATUS LISTS
------------ -------------------------------- -------
apr Y NAME-3
jul Not Defined NAME-6
jul Y NAME-4,NAME-5
oct Y NAME-2