【发布时间】:2017-09-13 18:19:44
【问题描述】:
我在下面有这张表 我的目标是取 perDem、perGop、perInd 列的平均值,但只取最后三个基于日期的列。我如何编写这样的查询,我尝试使用 order by、group by 但无法获得结果。我需要每列的平均值,但只需要基于当天的最后三个条目。因此,对于阿拉巴马州来说,例如平均 perDem、perGop、PerInd,但如果按天排序,则只有最后三行。 这是我到目前为止所拥有的,但我需要这个仅根据为该特定状态提交的日期取最后三个的平均值。
select polls.state,evotes,avg(perDem),avg(perGOP),avg(perInd)
from polls,electoral
where electoral.state=polls.state
group by electoral.state,polls.state";
【问题讨论】:
-
您只列出了三列,但您的评论
but only the last three based on the day表明应该有更多。请澄清。 -
您的意思是您对每天最近的三个条目感兴趣吗?
-
是的,这就是我的意思。
-
你如何确定它们发生的顺序(以便我们可以确定哪些是最近的)?
-
这是一个查询,我目前有 select polls.state,evotes,avg(perDem),avg(perGOP),avg(perInd) from polls,electoral whereelectoral.state=polls。州组按选举.state,polls.state"; 它只取所有民意调查的平均值,但我需要最后三个