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 是无效标识符。我们不能为列使用一些列别名吗?

4

1 回答 1

1

如果您想使用区分大小写的标识符(几乎总是一个坏主意),那么对该标识符的每个引用都需要区分大小写。在您的情况下,"Val"and"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
于 2013-03-20T21:58:07.257 回答