Oracle SQL:ROLLUP 求和不正确

发布于 2024-08-27 13:10:20 字数 2005 浏览 10 评论 0原文

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

查询的输出如下所示。黄色的“单位”列的总和为 53,但汇总显示为 51。单位数量相加正确...

替代文本 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)

Rollup seems to be working correcly to count the number of units, but not the number of trains. Any idea what could be causing that?

The output from the query looks like this. The sum of the Units column in yellow is 53 but the rollup is showing 51. The number of units adds up correctly though...

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

And here's the oracle SQL query...

 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2024-09-03 13:10:20

我认为这是独特的。
您获得的是 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

I think it is the DISTINCT.
You are getting the total number of DISTINCT values, not the sum of the count of distinct values for each record.

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

gives

OWNER     C1    C2
CTXSYS     6    82
MDSYS     11   653
-         11   735
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文