Hi, wondering if someone could help me out with an SQL query. I need to
select the most recent row in every group (grouped by group#)
group#/name/date(rail's timestamp)
1/bob/2008-07-30 16:00:43 UTC
2/joe/2008-07-30 16:00:43 UTC
1/john/2008-07-31 16:00:43 UTC
2/ed/2008-07-31 16:00:43 UTC
3/bill/2008-07-31 16:00:43 UTC
3/tom/2008-07-31 18:00:43 UTC
So I need:
1/john/2008-07-31 16:00:43 UTC
2/ed/2008-07-31 16:00:43 UTC
3/tom/2008-07-31 18:00:43 UTC
Hi, wondering if someone could help me out with an SQL query. I need to
select the most recent row in every group (grouped by group#)
group#/name/date(rail's timestamp)
1/bob/2008-07-30 16:00:43 UTC
2/joe/2008-07-30 16:00:43 UTC
1/john/2008-07-31 16:00:43 UTC
2/ed/2008-07-31 16:00:43 UTC
3/bill/2008-07-31 16:00:43 UTC
3/tom/2008-07-31 18:00:43 UTC
So I need:
1/john/2008-07-31 16:00:43 UTC
2/ed/2008-07-31 16:00:43 UTC
3/tom/2008-07-31 18:00:43 UTC
Thanks!
This might work - keep in mind that I am not working with any dates and
therefore have no way to test, but. . .
Object.find(:first, :conditions => ['group# = ? and ORDER BY date DESC',
group#])
If you are trying to do it in pure SQL is would be something like:
SELECT FIRST(date) FROM table_name where group# = '1' and ORDER BY date
DESC
Again this may or may not be correct but hopefully it gives you a start.
You check out the w3c website for SQL syntax, that's what I ues a lot:
def search(search, page)
paginate_by_sql ['SELECT t.* FROM posts t INNER JOIN ( SELECT
requirement_id, MAX(created_at) as MaxDate FROM posts GROUP BY
requirement_id )t1 ON t1.requirement_id=t.requirement_id AND
t1.MaxDate=t.created_at ORDER BY created_at DESC'], :per_page => 15,
:page => page
end
I have one question though... where and how would I include a WHERE
clause to find an optional 'search term'?