1

重複の可能性:
WHERE 句でのエイリアスの使用

以下のクエリの where 条件でエイリアス名を使用できない理由を教えてください..

SELECT * FROM (        
    SELECT USER_NAME AS "User ID",
         OLD_VALUE_STRING as "Status",
         NVL(lag(ENTRY_DATE) over (partition by user_name order by ENTRY_DATE), trunc(Sysdate-1)) as "Begin", 
           ENTRY_DATE as "End"
         FROM (SELECT U.USER_NAME,
                 U.FULL_NAME,
                 UH.OLD_VALUE_STRING,
                 UH.NEW_VALUE_STRING,
                 UH.ENTRY_DATE,
                 US.IS_WORKING
            FROM VIW.USER_HISTORY UH, VIW.USERS U, VIW.USER_STATUS US
           WHERE U.ID = UH.USER_ID
             AND UH.EVENT_ID = VIW.EVENT_PKG.GET_ID('UPD_USR_STATUS')
             AND UH.NEW_VALUE_STRING = US.ABBREVIATION
             AND UH.ENTRY_DATE >= TO_DATE('01082013', 'MMDDYYYY')
             AND UH.ENTRY_DATE <= TO_DATE('01092013', 'MMDDYYYY')
           ORDER BY U.USER_NAME, UH.ENTRY_DATE) 

    UNION        

    SELECT USER_NAME AS "User ID",
         NEW_VALUE_STRING as "Status",
         ENTRY_DATE AS "Begin", 
           lead(ENTRY_DATE) over (partition by user_name order by ENTRY_DATE) as "End"
           FROM (SELECT U.USER_NAME,
                 U.FULL_NAME,
                 UH.OLD_VALUE_STRING,
                 UH.NEW_VALUE_STRING,
                 UH.ENTRY_DATE,
                 US.IS_WORKING
            FROM VIW.USER_HISTORY UH, VIW.USERS U, VIW.USER_STATUS US
           WHERE U.ID = UH.USER_ID
             AND UH.EVENT_ID = VIW.EVENT_PKG.GET_ID('UPD_USR_STATUS')
             AND UH.NEW_VALUE_STRING = US.ABBREVIATION
             AND UH.ENTRY_DATE >= TO_DATE('01082013', 'MMDDYYYY')
             AND UH.ENTRY_DATE <= TO_DATE('01092013', 'MMDDYYYY')
           ORDER BY U.USER_NAME, UH.ENTRY_DATE)               
)  as innerTable
           WHERE Status <>'LOGGED_OUT'     
           ORDER BY 1,3 
4

2 に答える 2

3

FROM、JOINS、および WHERE 内のすべてのものを使用して、SELECT のすべてのものを生成します。したがって、WHERE は論理的に前にあるため、WHERE で SELECT のエイリアスを使用することはできません。SELECT に提示される行は、WHERE が既に適用された後に来ます。

そのため、WHERE にはまったくStatus知られていません。

于 2013-01-09T20:11:41.510 に答える
1

私は自分で解決策を見つけました。お時間をいただきありがとうございます。

SELECT * FROM (        
    SELECT USER_NAME AS "User ID",
         OLD_VALUE_STRING as "STATUS",
         NVL(lag(ENTRY_DATE) over (partition by user_name order by ENTRY_DATE), trunc(Sysdate-1)) as "Begin", 
           ENTRY_DATE as "End"
         FROM (SELECT U.USER_NAME,
                 U.FULL_NAME,
                 UH.OLD_VALUE_STRING,
                 UH.NEW_VALUE_STRING,
                 UH.ENTRY_DATE,
                 US.IS_WORKING
            FROM VIW.USER_HISTORY UH, VIW.USERS U, VIW.USER_STATUS US
           WHERE U.ID = UH.USER_ID
             AND UH.EVENT_ID = VIW.EVENT_PKG.GET_ID('UPD_USR_STATUS')
             AND UH.NEW_VALUE_STRING = US.ABBREVIATION
             AND UH.ENTRY_DATE >= TO_DATE('01082013', 'MMDDYYYY')
             AND UH.ENTRY_DATE <= TO_DATE('01092013', 'MMDDYYYY')
           ORDER BY U.USER_NAME, UH.ENTRY_DATE) 

    UNION        

    SELECT USER_NAME AS "User ID",
         NEW_VALUE_STRING as "STATUS",
         ENTRY_DATE AS "Begin", 
           lead(ENTRY_DATE) over (partition by user_name order by ENTRY_DATE) as "End"
           FROM (SELECT U.USER_NAME,
                 U.FULL_NAME,
                 UH.OLD_VALUE_STRING,
                 UH.NEW_VALUE_STRING,
                 UH.ENTRY_DATE,
                 US.IS_WORKING
            FROM VIW.USER_HISTORY UH, VIW.USERS U, VIW.USER_STATUS US
           WHERE U.ID = UH.USER_ID
             AND UH.EVENT_ID = VIW.EVENT_PKG.GET_ID('UPD_USR_STATUS')
             AND UH.NEW_VALUE_STRING = US.ABBREVIATION
             AND UH.ENTRY_DATE >= TO_DATE('01082013', 'MMDDYYYY')
             AND UH.ENTRY_DATE <= TO_DATE('01092013', 'MMDDYYYY')
           ORDER BY U.USER_NAME, UH.ENTRY_DATE)               
) 
           WHERE STATUS <>'LOGGED_OUT'     
           ORDER BY 1,3 
于 2013-01-09T20:34:27.997 に答える