Skip to content
Advertisement

SQL query Compare two WHERE clauses using same table

I am looking to compare two sets of data that are stored in the same table. I am sorry if this is a duplicate SO post, I have read some other posts but have not been able to implement it to solve my problem.

I am running a query to show all Athletes and times for the most recent date (2017-05-20):

I would like to compare the time achieved on the 20th May with the previous time for each athlete.

How can I structure my query showing all of the ATHLETES, TIME on 13th, TIME on 20th

I have tried some methods such as UNION ALL for example

Advertisement

Answer

You can get the previous time using a correlated subquery:

For performance, you want an index on (runner, eventid, date, time).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement