mysql - How to select the latest record of each hour in a day -


i want select latest record of each hour in given date, based on datetime column 'reading_on'. have executed below query

hourly_max =   inverterreading                .where("date(reading_on) = ? , imei = ?", date.today, "770000000000126")                .group("hour(reading_on)")                .having("max(hour(reading_on))")  hourly_max.group_by(&:id).each { |k,v| puts v.last.reading_on } 

in above query not getting required result. proper way select latest record of each hour in day. below table structure

enter image description here

select     hour(a.reading_on) hr, max(a.id),a.reading_on     inverterreadings left join    inverterreadings b on         year(a.reading_on)=year(b.reading_on)         , month(a.reading_on)=month(b.reading_on)         , day(a.reading_on)=day(b.reading_on)         , hour(a.reading_on)=hour(b.reading_on) ,      a.reading_on < b.reading_on      b.reading_on null group a.reading_on; 

demo : http://sqlfiddle.com/#!2/49a69/14


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -