Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Complicated query help

Author  Topic 

bpsintl
Posting Yak Master

132 Posts

Posted - 2008-10-27 : 07:22:58
Hi, I need help with a complicated query.

I have 2 tables in question, members and swim_times.

The tables are joined together on members.member_id = swim_times.memberid but here's where it gets complicated:

There are the following fields in the swim_times table

eventid (int)
pool_size (int)
location (varchar)
stroke (int)
swim_time (varchar)

Now, this table is for storing swimmers swim times. The eventid is from another table and holds the swim distances like 25m, 100m, 200m etc

The pool_size is either 50 or 25

The location is a free text field that the user can input when adding the swim time

The stroke field is either 1, 2, 3, 4 or 5

The swim_time is a varchar field storing the swim times like 00:47:71 (mm:ss:ms)

I want to be able to select the swimmers best time (one result) for each of the variables like pool_size, stroke, eventid, location etc

Any ideas?

Here's the query that joins the tables

select swim_times.*, members.*
from members inner join swim_times on members.member_id = swim_times.memberid

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-27 : 07:41:05
[code]select m.memberid,
m2.eventid,
m2.pool_size,
m2.stroke,
m2.location,
CONVERT(varchar(8),m2.besttime,108)
from members m
inner join (select memberid,eventid,pool_size, stroke,location,min(dateadd(hh,0, swim_time)) as besttime
from swim_times
group by memberid,eventid,pool_size, stroke,location)m2
on m.memberid=m2.memberid[/code]
Go to Top of Page

bpsintl
Posting Yak Master

132 Posts

Posted - 2008-10-27 : 08:00:20
I should have said, I know this is not the right forum but my db is MySQL. Do you know the syntax that would work for MySQL?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-27 : 08:02:50
quote:
Originally posted by bpsintl

I should have said, I know this is not the right forum but my db is MySQL. Do you know the syntax that would work for MySQL?


Nope..i dont
In that case..post this in MySQL forums like www.dbforums.com
Go to Top of Page

bpsintl
Posting Yak Master

132 Posts

Posted - 2008-10-27 : 08:07:57
Cheers
Go to Top of Page
   

- Advertisement -