2013-10-25 30 views
-2

,有2臺SQL組和空

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 

我有一個問題:

打印員工(不是經理人)的名單,由一個月分組出生和身份 (也將空狀態替換爲'未定義')。

任何想法?

+0

?如果你分組,你無法獲得一個列表!沒有必要使用第二個表格,這兩個表格之間的關係是什麼?發佈示例數據以及迄今爲止嘗試過的內容 – SriniV

+1

您是否可以編輯您的問題並添加您嘗試過的代碼以獲得結果?我們不想爲那些甚至沒有嘗試過的人浪費我們的努力。 – Rachcha

回答

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; 
+0

全部不正確.... – user2834458

+0

用所有樣品提出正確的問題 – SriniV

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  
要打印管理器的列表或計數