Solved

how to get fastest lap time and driver iD per race

Posted on 2016-07-31
8
96 Views
Last Modified: 2016-08-01
hello as per title: how to get fastest lap time and driver iD per race

if i run
SELECT * FROM F1database.lapTimes

Open in new window

 i get

# raceId, driverId, lap, position, time, milliseconds
'841', '20', '1', '1', '1:38.109', '98109'
'841', '20', '2', '1', '1:33.006', '93006'
'843', '20', '3', '1', '1:32.713', '92713'


actual results in attached file  

I would love result of

raceid, fastesttime, driverId

so i 10 races i would get 10 results if 700 races i would get 700 results.....  hope that all makes sense and that you can help
result.csv
0
Comment
Question by:James Murrell
  • 3
  • 3
  • 2
8 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41736818
what is the actual data type of the lap time column?
are all the other columns int (integers)?
what version of MySQL are you using?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41736828
for this sample:
CREATE TABLE lapTimes
    (`raceId` int, `driverId` int, `lap` int, `position` int, `time` varchar(12), `milliseconds` int)
;
    
INSERT INTO lapTimes
    (`raceId`, `driverId`, `lap`, `position`, `time`, `milliseconds`)
VALUES
    (841, 20, 1, 1, '1:38.109', 98109),
    (841, 20, 2, 1, '1:33.006', 93006),
    (843, 20, 3, 1, '1:32.713', 92713)

Open in new window

select
l.*
from lapTimes l
inner join (
        select
              raceId
            , driverId
            , min(milliseconds) milliseconds
        from lapTimes
        group by
              raceId
            , driverId
  ) m on l.raceId = m.raceId and l.driverId = m.driverId
      and l.milliseconds = m.milliseconds
;

| raceId | driverId | lap | position |     time | milliseconds |
|--------|----------|-----|----------|----------|--------------|
|    841 |       20 |   2 |        1 | 1:33.006 |        93006 |
|    843 |       20 |   3 |        1 | 1:32.713 |        92713 |

Open in new window

     
select
      raceId
    , driverId
    , sum(milliseconds) / 1000 race_seconds
from lapTimes
group by
      raceId
    , driverId
;
| raceId | driverId | race_seconds |
|--------|----------|--------------|
|    841 |       20 |      191.115 |
|    843 |       20 |       92.713 |

Open in new window



select
  `time`
, left(`time`,locate(':',`time`)-1) mins
, substr(`time`,locate(':',`time`)+1,2)  secs
, right(`time`, length(`time`) - locate('.',`time`)) hundedths
, milliseconds / 1000
from lapTimes

|     time | mins | secs | hundedths | milliseconds / 1000 |
|----------|------|------|-----------|---------------------|
| 1:38.109 |    1 |   38 |       109 |              98.109 |
| 1:33.006 |    1 |   33 |       006 |              93.006 |
| 1:32.713 |    1 |   32 |       713 |              92.713 |

Open in new window

1
 
LVL 6

Accepted Solution

by:
bcnagel earned 500 total points
ID: 41736833
Paul Maxwell is right: it would be helpful to know data types of your columns and more general information about your environment.

I'm going to assume that your milliseconds column represents the milliseconds elapsed for the lap in question, and I'm going to assume that it is an integer (or other numeric) column. That being the case, does this query give you what you need?

select der.raceId, lt.driverId, der.fastestLap
	from F1database.lapTimes as lt
	join (
		select lt.raceId, min(lt.milliseconds) as fastestLap
			from F1database.lapTimes as lt
			group by lt.raceId
		) der
	on lt.raceId = der.raceId
		and lt.milliseconds = der.fastestLap
	order by lt.raceId

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41736836
darn, for fastest laptime one ignores the driverid.... oh well
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 6

Expert Comment

by:bcnagel
ID: 41736839
Yep, I'm guessing we ignore the driverId, Paul. On the other hand, until we hear back from James we're just making our best guesses as to what he's really looking for. :-)
0
 
LVL 31

Author Comment

by:James Murrell
ID: 41737037
wow thanks, sorry i had to sleep.

i get data from http://ergast.com/mrd/db/ 


I will review these later today.  I am in meetings and will get back to you guys
0
 
LVL 31

Author Comment

by:James Murrell
ID: 41737946
Hello all great answer i will be accepting bcnagel answer as it it the one that makes sense to me: Basically I understand it....


big thanks guys
0
 
LVL 31

Author Closing Comment

by:James Murrell
ID: 41737948
great answer, one i could read and understand.... thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Amazon Redshift 2 27
Linking a DMV to a database id/sql text in SQL server 2008 8 46
RDBMS and No sql database 4 46
Access 2010 Query Syntax 5 23
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now