Need help solving this SQL query
I can successfully compute the 200 day moving average for one ticker using a SQL query, I'm trying now to create a VIEW. I am stuck the following query seems开发者_JAVA百科 logical but there is a SQL error "#1054 - Unknown column 'equity.TickerID' in 'where clause'"
SELECT AVG(Close) AS MA200 FROM equity
INNER JOIN
(
SELECT Close
FROM equity_pricehistory
WHERE TickerID = equity.TickerID
ORDER BY Timestamp
DESC LIMIT 0,200
) as Y
Got it. This will do the trick.
SELECT equity.id, AVG( equity_pricehistory.close ) AS MA200
FROM equity
INNER JOIN equity_pricehistory ON equity_pricehistory.TickerID = equity.id
GROUP BY equity_pricehistory.TickerID
ORDER BY Timestamp DESC
LIMIT 0 , 200
One thing I would point out is that you could totally ignore the whole equity
table if you already know all the TickerIDs you want are in equity_pricehistory
.
精彩评论