I have a time series of values in a sql table. One value per day. I need to calculate…
- User
I have a time series of values in a sql table. One value per day. I need to calculate what percentile today's value is amongst the last 40 days. How to do this?
- Assistant
To calculate the percentile of today's value among the last 40 days in SQL, you can use a window function along with the
PERCENT_RANK()orNTILE()function, depending on your SQL dialect and specific requirements. Below, I'll provide examples for both PostgreSQL and SQL Server, as they handle window functions slightly differently.