简体   繁体   中英

SQL average of previous range of columns into current column

I am trying to get the following calculations but at row level, in the image below I calculated the avg of values for each day (it can have n number of rows) then I used the LAG function to insert the avg of the previous row into the next row LAG_VAL column.

按计算分组

Now I am doing the calculations at row level, I have been able to get the average for that range of data using windowed functions (analytics)

ROUND(AVG(SUMCOUNTSFT3) OVER (partition by to_date(to_char(DATETIMEOFREADING, 'DD/MM/RRRR'))),2) as AVG_SUMCOUNTSFT3

but I have been not able to calculate the avg value of the previous day an insert that into the range of the next day as illustrated in the previous image.

行级计算

Not sure if there is a way to implement this with the RANGE function of if I need to use PLSQL.

Off the top of my head (without a matching schema to test with) this windowing clause should work:

(partition by to_date(to_char(DATETIMEOFREADING, 'DD/MM/RRRR') order by dates range between interval '1' day preceding and interval '1' day preceding)

This is plain SQL, so it works inside as well as outside of PL/SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM