SQL ..别名在where条件中

时间:2013-01-09 20:05:16

标签: sql oracle

  

可能重复:
  Using an Alias in a WHERE clause

请让我知道为什么我无法在以下查询的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 

2 个答案:

答案 0 :(得分:3)

FROMs和JOINS以及WHERE中的所有内容都用于生成SELECT的所有内容。因此,您不能在WHERE中使用SELECT中的别名,因为WHERE在逻辑上是先验的。呈现给SELECT的行在WHERE已经应用之后出现。

所以WHERE根本不知道Status

答案 1 :(得分: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