为什么AVG,MAX,MIN会返回不同的结果?

时间:2017-06-20 19:49:30

标签: sql oracle

我有一个查询,其中我想返回特定列的平均值,最大值和最小值。但是,当我执行两次或更多次时,结果彼此不同 - 这意味着每次我在同一数据集上运行查询时,我会得到不同的平均结果。

为什么?

下面是代码:

   WITH avr AS (
        SELECT  
         ticker_symb, 
         day_sum, 
         cusip, 
         clos_prc, 
         nclos_prc,
              case
                   when clos_prc is null and nclos_prc is not null 
                   then (nclos_prc - LAG( nclos_prc ignore nulls) OVER (Partition by  cusip  ORDER BY cusip asc)) 
                   when  clos_prc is not null and nclos_prc is null
                   then LEAD( nclos_prc ignore nulls) OVER (Partition by cusip  ORDER BY cusip asc)- LAG( naclos_prc ignore nulls) OVER (Partition by cusip  ORDER BY cusip)
                  else NULL
                  end diff
            from  DAILY_SUMMARY 
            where (cusip in (select distinct cusip from thistory where 
        td between to_date('1-JAN-2017') and to_date('10-JUN-2017'))))
            SELECT  ticker_symb, 
            day_sum, 
            cusip, 
            clos_prc, 
            nclos_prc,
            diff,

            AVG(diff) OVER() as avr,
            MAX(diff) OVER() as max_diff,
            MIN(diff) OVER() as min_diff ,

          FROM avr
          where day_sum >'1-JAN-2017'
          ORDER BY cusip;

1 个答案:

答案 0 :(得分:0)

我相信你应该在你正在寻找缺席收盘价的超过条款中通过 day_sum 订购。通过同一列进行分区和排序当然不常见,并且很可能这是导致不一致的原因。

如果没有数据可以使用,我已经猜到了下面的排序,但希望它足以让您进行试用/测试。

WITH avr
AS (
 SELECT
    ticker_symb
  , day_sum
  , cusip
  , clos_prc
  , nclos_prc
  , CASE 
   WHEN clos_prc IS NULL AND nclos_prc IS NOT NULL
    THEN (nclos_prc 
       - LAG(nclos_prc ignore nulls) OVER ( PARTITION BY cusip ORDER BY day_sum DESC )
         )
   WHEN clos_prc IS NOT NULL AND nclos_prc IS NULL
    THEN LEAD(nclos_prc ignore nulls) OVER ( PARTITION BY cusip ORDER BY day_sum ASC ) 
       - LAG(naclos_prc ignore nulls) OVER ( PARTITION BY cusip ORDER BY day_sum DESC )
   ELSE NULL
   END diff
 FROM DAILY_SUMMARY
 WHERE (
   cusip IN (
    SELECT DISTINCT cusip
    FROM thistory
    WHERE td BETWEEN to_date('1-JAN-2017') AND to_date('10-JUN-2017')
    )
   )
 )
select ...