-1

IIf(VW_GE_ALL_YEARQRY.DEP="AG","AG","OT") AS DEPT转换为 Oracle 时遇到问题

Select
VW_GE_ALL_YEARQRY.year,
VW_GE_ALL_YEARQRY.LOC,
VW_GE_ALL_YEARQRY.CAT,
VW_GE_ALL_YEARQRY.TY,
(case when VW_GE_ALL_YEARQRY.DEP = 'AG' then 'AG' else 'OT' end) as DEPT,
VW_GE_ALL_YEARQRY.SRCE,
Sum(VW_GE_ALL_YEARQRY.EXPEND * 0.001) As EXP
From
VW_GE_ALL_YEARQRY
GROUP BY VW_GE_ALL_YEARQRY.year, VW_GE_ALL_YEARQRY.LOC, VW_GE_ALL_YEARQRY.CAT, VW_GE_ALL_YEARQRY.TY, (case when VW_GE_ALL_YEARQRY.DEP = 'AG' then 'AG' else 'OT' end) as DEPT, VW_GE_ALL_YEARQRY.SRCE
HAVING (((Sum(VW_GE_ALL_YEARQRY.EXPEND * 0.001)))<>0);

查询告诉我我有一个错误,此时似乎微不足道

Error at Command Line:11 Column:169
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:
4

3 回答 3

1

如果唯一的问题是文本中的问题,那么这很容易。使用case(这是 ANSI 标准):

(case when VW_GE_ALL_YEARQRY.DEP = 'AG' then 'AG' else 'OT' end) as DEPT

这是您的查询的完整版本:

Select v.year, v.LOC, v.CAT, v.TY,
       (case when VW_GE_ALL_YEARQRY.DEP = 'AG' then 'AG' else 'OT' end) as DEPT
       v.SRCE,
       Sum(v.EXPEND * 0.001) As EXP
From VW_GE_ALL_YEARQRY v
GROUP BY v.year, v.LOC, v.CAT, v.TY,
         (case when VW_GE_ALL_YEARQRY.DEP = 'AG' then 'AG' else 'OT' end), v.SRCE
HAVING Sum(v.EXPEND * 0.001) <> 0;

我通过使用表别名对其进行了简化。我as DEPTgroup by子句中删除了。而且,我用单引号字符串替换了双引号字符串。

于 2013-07-23T18:11:43.973 回答
1

您可以使用 DECODE() 或更好的 CASE WHEN

于 2013-07-23T18:13:12.523 回答
0

你可以更换

IIf(VW_GE_ALL_YEARQRY.DEP="AG","AG","OT")

CASE WHEN VW_GE_ALL_YEARQRY.DEP='AG' THEN 'AG' ELSE 'OT' END

分享和享受。

于 2013-07-23T18:25:52.723 回答