2016-08-24 77 views
0

我使用此查詢如何更改日期

SELECT 
    A.EMP_NO, A.EMP_NAME, A.DEPARTMENT_NAME, A.ATT_DATE, 
    CASE 
     WHEN B.EMP_NO IS NULL 
      THEN 'A' 
      ELSE 'P' 
    END PRESENT_STATUS 
FROM 
    (SELECT 
     A.EMP_NO, A.EMP_NAME, A.DEPARTMENT_NAME, 
     B.ATT_DATE 
    FROM 
     EMPLOYEES A, 
     (SELECT 
       TO_DATE(:P_FROM_DATE) + LEVEL ATT_DATE 
      FROM DUAL 
      CONNECT BY LEVEL <= TO_DATE(:P_TO_DATE) - TO_DATE(:P_FROM_DATE)) B 
    WHERE 
     TRIM(TO_CHAR(B.ATT_DATE, 'DAY')) <> 'SUNDAY') A, 
     (SELECT EMP_NO, EMP_NAME, DEPARTMENT_NAME, TRUNC(TIME_STAMP) ATT_DATE 
     FROM EMP22 
     GROUP BY EMP_NO, EMP_NAME, DEPARTMENT_NAME, TRUNC(TIME_STAMP)) B 
WHERE A.EMP_NO = B.EMP_NO(+) AND A.ATT_DATE = B.ATT_DATE(+) 
GROUP BY A.EMP_NO, A.EMP_NAME, A.DEPARTMENT_NAME, A.ATT_DATE, B.EMP_NO 

查詢的結果是這樣的

| EMP_Name | emp_NO | Department_name | 2013-06-01 | 2013-06-02 | 2013-06-03 | 2013-06-04 | 2013-06-05 | 
| Naren |  1 |   22 |   A |  A |   A |   A |   P |   
| Srinu |  2 |   22 |   P |  P |   P |   P |   P |   
| Blah |  3 |   22 |   A |  P |   P |   P |   P | 

我需要得到這樣的日期應該是這樣的

| EMP_Name | emp_NO | Department_name | 01 | 02 | 03 |04 | 05 | 
| Naren |   1 |      22 |    A | A | A | A | P |   
| Srinu |   2 |      22 |    P | P | P | P | P |   
| Blah |    3 |      22 |     A | P | P | P | P | 

能你請幫我修改我的查詢

+4

該查詢不會生成該輸出。這只是你正在運行的一部分嗎? –

+0

正如Alex所說,輸出與該查詢不匹配。我還會注意到,在不改變查詢邏輯的情況下,列名可以被別名爲任何你想要的。 –

+0

[踢壞的習慣:使用舊式JOIN](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins。 aspx) - 在ANSI - ** 92 ** SQL標準(**超過20年前的**)中,舊式*逗號分隔的表*樣式列表被替換爲* proper * ANSI'JOIN'語法並且它的使用是不鼓勵的 –

回答

1

T ry this:

SELECT 
    A.EMP_NO, A.EMP_NAME, A.DEPARTMENT_NAME, to_char(A.ATT_DATE, 'dd'), 
    CASE 
     WHEN B.EMP_NO IS NULL 
      THEN 'A' 
      ELSE 'P' 
    END PRESENT_STATUS 
FROM 
    (SELECT 
     A.EMP_NO, A.EMP_NAME, A.DEPARTMENT_NAME, 
     B.ATT_DATE 
    FROM 
     EMPLOYEES A, 
     (SELECT 
       TO_DATE(:P_FROM_DATE) + LEVEL ATT_DATE 
      FROM DUAL 
      CONNECT BY LEVEL <= TO_DATE(:P_TO_DATE) - TO_DATE(:P_FROM_DATE)) B 
    WHERE 
     TRIM(TO_CHAR(B.ATT_DATE, 'DAY')) <> 'SUNDAY') A, 
     (SELECT EMP_NO, EMP_NAME, DEPARTMENT_NAME, TRUNC(TIME_STAMP) ATT_DATE 
     FROM EMP22 
     GROUP BY EMP_NO, EMP_NAME, DEPARTMENT_NAME, TRUNC(TIME_STAMP)) B 
WHERE A.EMP_NO = B.EMP_NO(+) AND A.ATT_DATE = B.ATT_DATE(+) 
GROUP BY A.EMP_NO, A.EMP_NAME, A.DEPARTMENT_NAME, A.ATT_DATE, B.EMP_NO