具有不同功能的聚集和分析的不同结果

时间:2016-07-27 09:23:07

标签: sql oracle window-functions

我无法理解为什么两个查询之间的结果存在差异:

1) select distinct sum(lot.lot_size) over (partition by lot.detail_id) buying_size, lot.f_detail_id
   from buying
   join lot on lot.lot_id = buying.lot_id
   where exists (select 1
                 from selling s
                 join buying b on b.buying_id = s.buying_buying_id
                 where s.deal_id = 123456
                 and buying.deal_id = b.deal_id 
                 and s.selling_detailid = buying.buying_detailid)
    and buying.buying_status <> 'Canceled'

结果:

    |buying_size|f_detail_id |
    |-----------|------------|
    |    105    |     1      |
    |    200    |     2      |
    |    75     |     3      |
    |    225    |     4      |
    |    300    |     5      |

2) select distinct *
   from (select sum(lot.lot_size) over (partition by lot.detail_id) buying_size, lot.f_detail_id
       from buying
       join lot on lot.lot_id = buying.lot_id
       where exists (select 1
                     from selling s
                     join buying b on b.buying_id = s.buying_buying_id
                     where s.deal_id = 123456
                     and buying.deal_id = b.deal_id 
                     and s.selling_detailid = buying.buying_detailid)
        and buying.buying_status <> 'Canceled')

结果:

    |buying_size|f_detail_id |
    |-----------|------------|
    |    105    |     1      |
    |    200    |     2      |
    |    75     |     3      |
    |    225    |     4      |
    |    150    |     5      |

我知道这个查询可能是用另一种方式用GROUP BY编写的,但我感兴趣的主要是为什么分析函数的结果依赖于DISTINCT。

0 个答案:

没有答案