他们是2桌
t_employee
ID NUMBER,
DEPARTMENT_ID NUMBER,
CHIEF_ID NUMBER,
NAME VARCHAR2(100 BYTE),
SALARY NUMBER,
BIRTH_DATE DATE,
ADDRESS VARCHAR2(200 BYTE),
STATUS VARCHAR2(1 BYTE)
和
t_department
ID NUMBER,
NAME VARCHAR2(100 BYTE)
BIRTH_DATE format is
day.month.year
STATUS column is
Y or null
我有一个问题:
打印按出生月份和状态分组的员工(不是经理)列表 (也将null状态替换为'Not Defined')。
有什么想法吗?
答案 0 :(得分:0)
这样的事情:
WITH T_EMPLOYEE
AS (SELECT
1 AS ID,
2 AS CHIEF_ID,
SYSDATE
- 15000
AS BIRTH_DATE,
'Y' AS STATUS
FROM
DUAL
UNION ALL
SELECT
2 AS ID,
3 AS CHIEF_ID,
SYSDATE
- 18000
AS BIRTH_DATE,
'Y' AS STATUS
FROM
DUAL
UNION ALL
SELECT
3 AS ID,
NULL AS CHIEF_ID,
SYSDATE
- 19000
AS BIRTH_DATE,
NULL AS STATUS
FROM
DUAL
UNION ALL
SELECT
4 AS ID,
3 AS CHIEF_ID,
SYSDATE
- 17000
AS BIRTH_DATE,
NULL AS STATUS
FROM
DUAL
UNION ALL
SELECT
5 AS ID,
3 AS CHIEF_ID,
SYSDATE
- 18000
AS BIRTH_DATE,
NULL AS STATUS
FROM
DUAL)
SELECT
ID,
TRUNC ( BIRTH_DATE ) AS DOB,
NVL ( STATUS, 'Not Defined' ) AS STATUS
FROM
T_EMPLOYEE
WHERE
ID NOT IN (SELECT DISTINCT NVL ( CHIEF_ID, 0 ) FROM T_EMPLOYEE)
ORDER BY
1;
答案 1 :(得分:0)
希望这可以帮助你:
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