2013-03-20 83 views
1
select k.Val, sum(k.Cnt) "Cnt" from 
(
(select a.Env_Location "Val", count(a.VolumeID) "Cnt" 
    from DTree 
    join ZCOP_APLNG_Documents a on 
    DTree.DataID = a.DataID and DTree.VersionNum = a.VersionNum 
    where 
    DTree.OwnerID = -2111 and 
    DTree.SubType not in (0, 136) and 
    a.Env_Location is not NULL 
    group by a.Env_Location 
    ) 

union 
    (select 
    b.Env_Location "Val", count(b.VolumeID) "Cnt" 
    from DTree 
    join ZCOP_APLNG_Corr b on 
    DTree.DataID = b.DataID and DTree.VersionNum = b.VersionNum 
    where 
    DTree.OwnerID = -2111 and 
    DTree.SubType not in (0, 136) and 
    b.Env_Location is not NULL 
    group by b.Env_Location 
    ) 
) k  
    group by k.Val 

任何人都可以幫助我完成這項工作。顯示錯誤Val或Cnt是無效標識符。我們不能使用列的別名嗎?列別名不能在Oracle中的外部查詢中訪問

回答

1

如果您想使用case-sensitive identifiers(幾乎總是一個壞主意),那麼每個對該標識符的引用都需要區分大小寫。在你的情況下,"Val""Cnt"都是區分大小寫的標識符,所以你需要每次使用區分大小寫的語法引用它們。類似於

SELECT k."Val", sum(k."Cnt") "Cnt" from 
    ... 
GROUP BY k."Val" 

在絕大多數情況下,您確實不希望使用區分大小寫的別名。你通常會更好地服務與

SELECT k.val, sum(k.cnt) cnt from 
(
    SELECT a.env_location val, count(a.volumeID) cnt 
    ... 
    UNION 
    SELECT b.env_location val, count(b.volumeID) cnt 
    ... 
) k 
GROUP BY k.val 
+0

謝謝賈斯汀..我不知道區分大小寫的標識符。 – 2013-03-22 22:11:20