我需要分组年份,并选择每年平均值的最后五年,以便我有平均和年份输出

时间:2013-09-04 11:42:48

标签: java mysql

我需要分组年份,并选择每年平均值的最后五年,以便我有平均和年份输出

SELECT AP, yr
FROM (
SELECT Year AS yr, AVG( "tbltest.AskingPrice" ) AS AP
FROM tbltest
GROUP BY `yr` DESC
LIMIT 5
)sub
GROUP BY `yr` ASC 

2 个答案:

答案 0 :(得分:0)

我认为你的子查询符合你的要求:

SELECT Year AS yr, AVG( "tbltest.AskingPrice" ) AS AP
FROM tbltest
GROUP BY `yr`
ORDER BY Year DESC
LIMIT 5;

我刚将desc移到order by以与标准SQL保持一致。

答案 1 :(得分:0)

SELECT sub.yr, sub.AP
FROM (
    SELECT Year AS yr, AVG(AskingPrice) AS AP
    FROM tbltest
    GROUP BY yr
    ORDER BY yr DESC
    LIMIT 0,5
) sub
ORDER BY sub.yr ASC