2013-03-20 55 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

相关问题