I have a mathematical code that requires two subqueries. Below is the copy of the code.
SELECT AVGTEMP_X.Average_Temp, ProduceMonAvg.Avergae_CPI,
SUM(AVGTEMP_X.Average_Temp) as AVGTEMPSUM,
SUM(ProduceMonAvg.Average_CPI) as PROCPISUM,
SUM(AVGTEMP_X.Average_Temp*AVGTEMP_X.Average_Temp) AS AVGTEMPSQ,
SUM(ProduceMonAvg.Average_CPI*ProduceMonAvg.Average_CPI)PROCPISQ,
SUM(AVGTEMP_X.Average_Temp*ProduceMonAvg.Average_CPI) AS PowerSum,
COUNT(*) as COUNT,
(COUNT*(PowerSum)-(AVGTEMPSUM))/(sqrt(COUNT*(AVGTEMPSQ)-pow(AVGTEMPSUM,2)*
(COUNT*(PROCPISQ)-pow(PROCPISUM,2)))) AS Correlation
FROM(SELECT series_id,FORMAT(AVG(value),2) AS Average_CPI, period
FROM CURRENT
WHERE series_id = 'CUSR0000SEFK'
AND theYear >=(1997)
AND theYear <=(2010)
GROUP BY period) AS ProduceMonAvg,
(SELECT Average_Temp
FROM AVGTEMP_X)
INNER JOIN ProduceMonAvg
ON AVGTEMP_X.period = ProduceMonAvg.period
I get an error saying
1146 - Table 'xxx.ProduceMonAvg' doesn't exist
1054 - Unknown column 'COUNT' in 'field list'
In your outer query, you reference ProdcueMonAvg.Average_CPI
etc. In your inner query, you alias it as AS ProduceMonAvg
. note the typo - Prodcue
vs Produce
. Are you sure your error doesn't say Table xxx.ProdcueMonAvg doesn't exist
?
For that matter, there are a couple of other things that seem wrong with your query. Your join to the AVGTEMP_X query is using a subquery where one isn't necessary, it's also performing a cross join and THEN an inner join. I doubt that is what you wanted. Try this one out:
SELECT AVGTEMP_X.Average_Temp,
ProduceMonAvg.Average_CPI,
SUM(AVGTEMP_X.Average_Temp) AS AVGTEMPSUM,
SUM(ProduceMonAvg.Average_CPI) AS PROCPISUM,
SUM(AVGTEMP_X.Average_Temp * AVGTEMP_X.Average_Temp) AS AVGTEMPSQ,
SUM(ProduceMonAvg.Average_CPI * ProduceMonAvg.Average_CPI) PROCPISQ,
SUM(AVGTEMP_X.Average_Temp * ProduceMonAvg.Average_CPI) AS PowerSum,
COUNT(*) AS TOTAL,
(COUNT(*) * (SUM(AVGTEMP_X.Average_Temp * ProduceMonAvg.Average_CPI)) - (SUM(AVGTEMP_X.Average_Temp))) / (sqrt(COUNT(*)* (SUM(AVGTEMP_X.Average_Temp * AVGTEMP_X.Average_Temp)) - pow(SUM(AVGTEMP_X.Average_Temp), 2) * (COUNT(*)* (SUM(ProduceMonAvg.Average_CPI * ProduceMonAvg.Average_CPI)) - pow(SUM(ProduceMonAvg.Average_CPI), 2)))) AS Correlation
FROM (
SELECT series_id,
FORMAT(AVG(value), 2) AS Average_CPI,
period
FROM CURRENT
WHERE series_id = 'CUSR0000SEFK'
AND theYear >= (1997)
AND theYear <= (2010)
GROUP BY period
) AS ProduceMonAvg
INNER JOIN AVGTEMP_X ON AVGTEMP_X.period = ProduceMonAvg.period
I've also created some mock tables and data and finally tested the query. It returns results. You can see it here: http://sqlfiddle.com/#!9/47523/14
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.