-2

它们是 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

我有一个问题:

打印按出生月份和状态分组的员工(不是经理)列表(也将空状态替换为“未定义”)。

任何想法?

4

2 回答 2

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;
于 2013-10-25T09:04:48.760 回答
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      
于 2013-10-25T09:13:05.720 回答