2010-03-30 4 views
1

롤업이 열차 수가 아니라 단위 수를 계산하는 데 정확하게 작동하는 것처럼 보입니다. 어떤 아이디어가 그 원인 일 수 있습니까?Oracle SQL : ROLLUP이 올바르게 합산되지 않았습니다.

쿼리 결과는 다음과 같습니다. ... 노란색 단위 컬럼의 합은 53이지만 롤업 단위의 수는 정확하게 생각까지 추가 (51)을 보여주는 ...

alt text http://img522.imageshack.us/img522/9057/ss20100330111503.png

그리고 여기에 오라클 SQL 쿼리의

select t.year, 
    t.week, 
    decode(t.mine_id,NULL,'PF',t.mine_id) as mine_id, 
    decode(t.product,Null,'LF',t.product) as product, 
    decode(t.mine_id||'-'||t.product,'-','PF',t.mine_id||'-'||t.product) as code, 
    count(distinct t.tpps_train_id) as trains, 
    count(1) as units 

from 

(
    select trn.mine_code as mine_id, 
      trn.train_tpps_id as tpps_train_id,  
      round((con.calibrated_weight_total - con.empty_weight_total),2) as tonnes 
    from widsys.train trn 
       INNER JOIN widsys.consist con 
        USING (train_record_id) 

    where trn.direction = 'N' 
      and (con.calibrated_weight_total-con.empty_weight_total) > 10 
      and trn.num_cars > 10 
    and con.consist_no not like '_L%' 
    ) w, 

    (
     select to_char(td.datetime_act_comp_dump-7/24, 'IYYY') as year, 
      to_char(td.datetime_act_comp_dump-7/24, 'IW') as week, 
      td.mine_code as mine_id, 
      td.train_id as tpps_train_id, 
      pt.product_type_code as product 
     from tpps.train_details td 
      inner join tpps.ore_products op 
      using (ore_product_key) 
      inner join tpps.product_types pt 
      using (product_type_key) 
     where to_char(td.datetime_act_comp_dump-7/24, 'IYYY') = 2010 
      and to_char(td.datetime_act_comp_dump-7/24, 'IW') = 12 
     order by td.datetime_act_comp_dump asc 
) t 
where w.mine_id = t.mine_id 
    and w.tpps_train_id = t.tpps_train_id 

    having t.product is not null or t.mine_id is null 
    group by 
      t.year, 
      t.week, 
     rollup(
      t.mine_id, 
      t.product) 

답변

3

나는 그것이 DISTINCT라고 생각한다. 각 레코드의 고유 한 값 수의 합계가 아닌 DISTINCT 값의 총 수를 얻고 있습니다. 1, 논리적 인 것 같다

select nvl(owner,'-') owner, count(distinct object_type) c1, count(*) c2 
from all_objects 
where owner in ('MDSYS','CTXSYS') 
group by rollup(owner) 

OWNER  C1 C2 
CTXSYS  6 82 
MDSYS  11 653 
-   11 735 
+0

@Gary주는 –