select comp_id,
(select dept_name from p_dept where p_dept.dept_id=comp_id) comp_name,
count(distinct serv_id),sum(fact_fee)/100,sum(months)--第四部
from tmp_adsl_charge_201203  --第一步
where oper_type_id='03040401' --第二步
group by comp_id,2  --第三步
order by 1,2;--第五步

解决方案 »

  1.   

    (select dept_name from p_dept where p_dept.dept_id=comp_id) 这个在SELECT子句中再关联p_dept和tmp_adsl_charge_201203,获得复合where条件的数据
      

  2.   

    执行顺序的序号,select comp_id,
           (select dept_name 
            from p_dept 
            where p_dept.dept_id=comp_id) 'comp_name', -- 3
           count(distinct serv_id),                    -- 3
           sum(fact_fee)/100,                          -- 3
           sum(months)                                 -- 3
     from tmp_adsl_charge_201203
     where oper_type_id='03040401'   -- 1
     group by comp_id,2              -- 2
     order by 1,2                    -- 4