I'm trying to write a single query using 3 tables.
The tables and their columns that I will be using are:
Sec – ID, Symbol
Hss – Code, HDate, Holiday
Fddd – ID, Date, Price
Given a symbol AAA
, I need to get the ID from the first table and match it with the ID from the third table. The second table's date must match the third table's dates with the condition of Code=1
and Holiday=1
.
The Dates in the second and third table are in ascending order with most recent dates at the bottom. I want to get the average 50 day
and 200 day
prices. The dates in the tables are in ascending order so I want to make it descending and select the top 50
and 200
to get the average prices.
So far I can only get one average. I cannot add a second SELECT TOP 50
or add a subquery
within the second avg()
.
SELECT AVG(TwoHun)TwoHunAvg --, AVG(Fifty) AS FiftyAvg
FROM (SELECT TOP 200 Fddd.price AS TwoHun --, TOP 50 Fddd.price AS Fifty
FROM Sec
JOIN Fddd
ON Sec.ID = Fddd.ID AND Sec.symbol = 'AAA'
JOIN Hss
ON Fddd.date = Hss.Hdate AND Hss.Code = 1 AND Hss.Holiday = 1
ORDER BY Fddd.Date DESC) AS tmp;
Thanks in advance!