user2473664
user2473664

Reputation: 71

Calculating running average with sqlite

I am using the following code to calculate a running average:

SELECT AVG(close) FROM daily
ORDER BY datum DESC
LIMIT 50

for a 50-day average. However, it doesn't seem to work. I can change LIMIT to 5, 10, 20. The result is always the same. Is the LIMIT just a visual limit maybe?

Upvotes: 2

Views: 1222

Answers (1)

Jaloopa
Jaloopa

Reputation: 750

LIMIT restricts your result set, not the set you're averaging over. You'd want something like

SELECT AVG(close) FROM
(
    SELECT close FROM daily ORDER BY datum DESC LIMIT 50
)

Upvotes: 4

Related Questions