select * from times as t
inner join (
select course, event_code, min(finals) as "min_time" from times
where id_competitor=XXXX and finals>0
group by course, event_code
) as tt
on t.event_code=tt.event_code
and t.course=tt.course
and t.finals=tt.min_time
and t.id_competitor=XXXX
order by t.course, t.event_code--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
for row in db(db.times.competitor==xxxx).select(
db.times.course, db.times.event_code, min_time, groupby=(db.times.course,db.times.event_code)):
print row.times.course, row[min_time]
Hi Richard --Well, it wasn't the solution I was originally thinking of, but you clued me into the solution that works. I used "executesql" and a big 'ol triple-quoted SQL string to get the query working. And the surprise (for me) was how easy it was to get the output into a Rows object where I could use the power of web2py to format the output for my view.The DAL is amazing and very broad in what it covers, but there are just certain corner-cases of SQL which will always elude it. I've decided that's a good thing. Software systems that try to do *everything* in a new paradigm tend to become so bloated with corner-cases that they lose their way. Our little web2py has such sparse beauty it would be a shame to see it try to turn into "SQL part deux".Thanks for your help and for spending some of your thought-photons on my problem.-- Joe B.
On Tuesday, February 18, 2014 6:59:45 AM UTC-8, Richard wrote:
So... You seem to be ready for a stored procedure... Or you need to thought to your problem into more then one step, maybe having a view for the sub query...I was to porpose the multiple subquery, since your query is already slow, I would try this option, just to see if it couldn't be a bit faster...For slow query that for which I don't want to invest more time to create a function or stored procedure, I generally create a view and cache it with web2py...Richard
Hi Derek --
Yes, I need more columns. I need the name of the meet where the best time was achieved. And the club the athlete was a member of, and the date of the fastest time, etc.
I can't put all of those columns in the group and ask for "min" of each because the min of each column will be from different rows of the table. I want the minimum time, AND the name of that exact meet -- not the minimum time and then the "min" across all meet names. That's why I need to find the min time in one query and then use that to look up the row where it occurs, so I can get the rest of the data.
See my finished page in my response to Richard and maybe it will make more sense. It's hard to put database stuff into words!