可能重复:
在 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