I have this table below my goal is to take the average of columns perDem,perGop,perInd but only the last three based on the day.How can I write such a query I tried to use order by,group by but am not able to achieve the results. I need an avg of each column but only the last three entries based on the day. so for Alabama for example average perDem,perGop,PerInd but only three rows which are last if ordered by day. This is what I have so far but I need this to only take the avg of the last three based on the day submitted for that particular state.
select polls.state,evotes,avg(perDem),avg(perGOP),avg(perInd)
from polls,electoral
where electoral.state=polls.state
group by electoral.state,polls.state";