I have a query in which I want to return the average, the max and the min of a specific column. However, when I execute twice or more times, the results are different from each other - meaning every time I run the query on the same data set I get different average results.
Why is that?
Heres the code:
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;