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 中使用来自 SELECT 的别名,因为 WHERE 在逻辑上是优先的。呈现给 SELECT 的行出现在 WHERE 已应用之后。

所以Status根本不知道 WHERE。

于 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 回答