我如何除以别名?

时间:2015-06-25 15:00:21

标签: sql oracle oracle11g

当我运行以下查询时,我收到ORA-00937错误。

select apex_utilities.get_invtype(o_code)
,count(c.id) as "Case Count"
,round(sum(billable_time/3600),2) as "Time(hrs)"
,round(sum(billable_time/count(c.id)),2) as "AHT(secs)"
from        myTable c
where c.date_created between :p31_period_start and add_months(:p31_period_start,1)
group by rollup(apex_utilities.get_invtype(o_code));

如果我注释掉该行

    ,round(sum(billable_time/count(c.id)),2) as "AHT(secs)"

它按预期运行,没有出现此列。

我如何让它包括这条线?如果我将billable_time添加到组,那么输出是不正确的,因为我得到很多行,因为它将所有billable_time和invtype分组。此外,我必须从行中删除“SUM”以使其运行。

我正在寻找的输出类似于以下

invtype    CaseCount    Time(Hrs)    AHT(secs)
CDCW          1234       308:53:45   909.56
CBCB          100        24:56:34    109.24

2 个答案:

答案 0 :(得分:1)

看起来你想要的实际上是:

 ,round(sum(billable_time)/count(c.id),2) as "AHT(secs)"` 

我怀疑它是因为你在聚合内聚合所致。如果你真的想要这样做,那么你可能需要一个子查询来获取count(c.id),这样你就可以在SUM(billable_time, count_of_c_id)位的聚合中应用它。

答案 1 :(得分:1)

您需要将该计数移到子查询中。为了清楚起见,我在这里总结了同样的事情:

 with cteTable as
 (
  select apex_utilities.get_invtype(o_code) invtype
        ,count(c.id)                        casecount
        ,sum(billable_time)                 totaltime
    from myTable c
   where c.date_created between :p31_period_start and add_months(:p31_period_start,1)
group by rollup(apex_utilities.get_invtype(o_code))
 )
 select invtype 
       ,casecount                    "Case Count"
       ,round(totaltime/3600,2)      "Time(hrs)"
       ,round(totaltime/casecount,2) "AHT(secs)"
   from cteTable;