Oracle SUM函数在Select子句中不起作用

时间:2014-09-17 13:04:06

标签: oracle function sum

我有下面附带的代码,即使我不使用SUM函数,sum(slice)列也会给出相同的结果。有人可以帮我修复代码吗?

还需要以下内容:

Currently, it is giving output as:

Task Name             Resource ID                  Slice Date            Slice Hours
abc                       123                         4/1                   8.50
abc                       123                         4/2                   7.50
abc                       123                         4/3                   8.50

我需要输出为:

Task Name          Resource ID                     Slice Date            Slice Hours
abc                       123                      4/1 – 4/30        <Total for the month>
xyz                       123                      4/1 – 4/30        <Total for the month>

select distinct PCF.project_code as project_id, 
    PCF.project_name as project_name, 
    RCF.resource_id, 
    RCF.last_name || ' ' || RCF.first_name as resource_name,
    Tsh.slice_date as fact_date,
    sum(tsh.slice) "Total Slice",
    T.prname as TaskName, 
    task_info.phase_code as phase_code, 
    task_info.phase_name as phase_name, 
    task_info.task_sequence as task_outline
from prj_blb_slicerequests tsr
JOIN  prj_blb_slices tsh ON tsr.id = tsh.slice_request_id
JOIN prtimeentry TE ON tsh.prj_object_id = te.prid
JOIN prtimesheet TS ON TS.prid = TE.prtimesheetid 
 JOIN  prAssignment A ON TE.prassignmentid = A.prid
 JOIN prtask T ON A.prtaskid = T.prid 
JOIN nbi_resource_current_facts RCF ON TS.prresourceid = RCF.resource_id 
 JOIN nbi_project_current_facts PCF ON T.prprojectid = PCF.project_id
 JOIN prj_projects PP ON PCF.project_id = PP.prid
 JOIN (select task_id, phase_code, phase_name, task_name, task_sequence from cust_phase_rollup_v) task_info ON T.prid = task_info.task_id
where tsr.request_name = 'Daily Timeentry Actuals' 
  and tsh.slice > 0 
  and ts.prstatus < 5
  and tsh.slice_date >= to_date('1-MAY-14') 
  and tsh.slice_date <= to_date('31-MAY-14')
group by project_code, project_name, resource_id, RCF.last_name || ' ' || RCF.first_name, Tsh.slice_date, T.prname, task_info.phase_code,
  task_info.phase_name, task_info.task_sequence
order by project_id

1 个答案:

答案 0 :(得分:0)

2个问题:

  1. 您希望每月,而不是每天都有结果,因此您应该将其转换为月/年格式,而不是仅选择slice_date

  2. 要使用聚合函数,您的查询中需要相应的group by。您应该按照您选择的所有其他列进行分组。

  3. 这应该可行(但未经测试):

    select distinct PCF.project_code as project_id, 
        PCF.project_name as project_name, 
        RCF.resource_id, 
        RCF.last_name || ' ' || RCF.first_name as resource_name,
        to_char(Tsh.slice_date, 'mm/yyyy') as fact_date,
        sum(tsh.slice) "Total Slice",
        T.prname as TaskName, 
        task_info.phase_code as phase_code, 
        task_info.phase_name as phase_name, 
        task_info.task_sequence as task_outline
    from prj_blb_slicerequests tsr
    JOIN  prj_blb_slices tsh ON tsr.id = tsh.slice_request_id
    JOIN prtimeentry TE ON tsh.prj_object_id = te.prid
    JOIN prtimesheet TS ON TS.prid = TE.prtimesheetid 
     JOIN  prAssignment A ON TE.prassignmentid = A.prid
     JOIN prtask T ON A.prtaskid = T.prid 
    JOIN nbi_resource_current_facts RCF ON TS.prresourceid = RCF.resource_id 
     JOIN nbi_project_current_facts PCF ON T.prprojectid = PCF.project_id
     JOIN prj_projects PP ON PCF.project_id = PP.prid
     JOIN (select task_id, phase_code, phase_name, task_name, task_sequence from cust_phase_rollup_v) task_info ON T.prid = task_info.task_id
    where tsr.request_name = 'Daily Timeentry Actuals' 
      and tsh.slice > 0 
      and ts.prstatus < 5
      and tsh.slice_date >= to_date('1-MAY-14') 
      and tsh.slice_date <= to_date('31-MAY-14')
      group by PCF.project_code, 
               PCF.project_name, 
               RCF.resource_id, 
               RCF.last_name || ' ' || RCF.first_name,
               to_char(Tsh.slice_date, 'mm/yyyy'),
               T.prname, 
               task_info.phase_code, 
               task_info.phase_name, 
               task_info.task_sequence
      order by PCF.project_code