Skip to content
Advertisement

SQL How would I show a rolling 3 month average for this query

Need some help with maths side of things with this bit of code. You may see I need help on even more! But any help you could give me would be great

Basically, my aim is to bring back an; – AccountID, – something called a SPID, of which there could be multiple in an account, – Billed Date – The amount of the invoice on the corresponding billed date – Then 3 Month Average

The last part is what I am having an issue with

So far I have done this

Now this gives me the raw data as so (apologies, not sure hwo to show in here, if someone can pleas edit that would be great);

However I would like a column at the end which would show the average of the cost for the previous 3 months

I have tried to use lead and lag functions, but I keep getting NULL in the column, which is where I think I’m gong wrong

any advice or help would be great

Thanks in advance

Advertisement

Answer

If there will always be a row, then seems like the easiest would be with a windows AVG function. Using the end sample values you have:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement