Oracle SQL Group无法正常工作

时间:2014-04-22 10:20:19

标签: sql oracle count group-by

我正在运行查询以返回每周完成的多个作业的计数,并根据单独的财务周期表将周分解为4周的周期,但查询未返回正确的计数。

以下是代码示例:

select (SELECT (fp.financialperiod || ' week ' ||
ceil(floor((wo.actfinish - p.periodstart+1))/7))
        FROM maximo.financialperiods fp
       WHERE TRUNC (wo.actfinish) BETWEEN fp.periodstart
                                      AND fp.periodend) fin_period,
wo.wo8 as assetgroup,
Count(wo.wonum)
from maximo.workorder wo
where (TRUNC (wo.actfinish) BETWEEN TO_DATE (:startdate, 'DD/MM/YYYY')
                                    AND TO_DATE (:enddate, 'DD/MM/YYYY'))
group by wo.actfinish,
wo.wo8

2 个答案:

答案 0 :(得分:0)

SELECT 
    (SELECT (fp.financialperiod || ' week ' || ceil(floor((wo.actfinish - p.periodstart+1))/7))
     FROM maximo.financialperiods fp
     WHERE TRUNC (wo.actfinish) BETWEEN fp.periodstart AND fp.periodend) fin_period, wo.wo8 as assetgroup, Count(wo.wonum) AS [Count]
FROM maximo.workorder wo
WHERE (TRUNC (wo.actfinish) BETWEEN TO_DATE (:startdate, 'DD/MM/YYYY')
                                    AND TO_DATE (:enddate, 'DD/MM/YYYY'))
GROUP BY wo.actfinish, wo.wo8, fp.financialperiod

答案 1 :(得分:0)

我怀疑原因是你需要按第一列聚合。鉴于查询的结构,使用子查询最容易完成:

select fin_period, wo.wo8, count(wo.wonum) as cnt
from (select (SELECT (fp.financialperiod || ' week ' || ceil(floor((wo.actfinish -    p.periodstart+1))/7))
              FROM maximo.financialperiods fp
              WHERE TRUNC (wo.actfinish) BETWEEN fp.periodstart AND fp.periodend
             ) as fin_period,
             wo.wo8 as assetgroup, wo.wonum
      from maximo.workorder wo
      where TRUNC(wo.actfinish) BETWEEN TO_DATE (:startdate, 'DD/MM/YYYY')
                                    AND TO_DATE (:enddate, 'DD/MM/YYYY')
     ) t
group by fin_period, wo.wo8;