【问题标题】:Oracle SQL: ROLLUP not summing correctlyOracle SQL:ROLLUP 未正确求和
【发布时间】:2011-02-02 07:34:46
【问题描述】:

Rollup 似乎可以正确计算单元数,而不是火车数。知道是什么原因造成的吗?

查询的输出如下所示。黄色的 Units 列的总和为 53,但汇总显示为 51。虽然单位数加起来是正确的...

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

这是 oracle 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)

【问题讨论】:

    标签: sql oracle rollup


    【解决方案1】:

    我认为这是 DISTINCT。 您得到的是 DISTINCT 值的总数,而不是每条记录的不同值的总数。

    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
    

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2015-02-15
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      相关资源
      最近更新 更多