posted on Thursday, May 25, 2006 10:51 AM by marathonsqlguy

RANK and processing Marathon Results

In my presentation on May 11 to the Ohio North SQL Server Special Interest Group (you can download my presentation and demo code from this site) I covered Transact-SQL enhancements in SQL Server 2005. I flew through some of the demos, partially due to time, but also partially because I haven't had a chance to really work with some of the features and I am unfamiliar with them. One such feature was the RANK function, and I used the example from Books Online. The example is not very revealing, so I decided to play a bit on my own.

Last weekend I ran the Fargo Marathon (and for a number of reasons managed to tie my Personal Worst). The event, however, made me think about the data possibilities and the rank function. Here's a sample of the results raw data from the race:

LastNameFirstNameSexAgeTimeDIVCityStateCountry
PoteetScottM3203:11:08M30-34 N Las Vegas, NV, USA
PoteetKristinF2903:34:43F25-29 N Las Vegas, NV, USA
JacksonDavidM4203:39:25M40-44 W Lafayette, IN, USA
RossLynnetteF4105:26:54F40-44 Mc Intosh
MoenBrandonM2202:37:44M20-24 Mankato, MN, USA
KennedyJamesM4102:59:07M40-44 Eagan, MN, USA
SteffensMontyM4703:02:21M45-49 Sartell, MN, USA
FallerGerryM4003:03:28M40-44 Maple Grove, MN, USA
WeluMikeM4003:03:53M40-44 Canistota, SD, USA
GrindallMegF2503:04:45F25-29 Fargo, ND, USA

I built a query to present the results ranking the finishers by their time within their Division like this:

select LastName, FirstName, Sex, Age, CONVERT(char(8), [Time], 108) as Time, DIV, 
  RANK() over (PARTITION by DIV order by [TIME]) as Place
FROM dbo.OverallResults
ORDER by DIV, [TIME]

The PARTITION clause tells the RANK function where to draw the lines in the ranking process, so by partitioning by division gives me the age group separation, then ordering by Time within the Division gives me the top finishers within each division. This process used to be quite complicated, and the new RANK function makes it a relatively simple process.

LastNameFirstNameSexAgeTimeDIVPlace
RebenitschSarahF1803:46:09F15-19 1
PedersonElizabethF1903:55:10F15-19 2
StepkaRachelF1904:15:01F15-19 3
MuellerCarrieF2203:15:36F20-24 1
BotnenAmyF2103:21:59F20-24 2
ElsethMandyF2403:22:51F20-24 3
GrindallMegF2503:04:45F25-29 1
KasparErinF2603:09:46F25-29 2
FieldJillF2603:13:03F25-29 3
CookTerriF3003:08:33F30-34 1
SawtelleMindyF3303:08:34F30-34 2
ReichLisaF3103:27:51F30-34 3
BlatherwickJossM1903:20:57M15-19 1
BaxterCodyM1903:27:38M15-19 2
NelsonJacobM1803:33:43M15-19 3
MoenBrandonM2202:37:44M20-24 1
NielsenAndyM2302:59:10M20-24 2
RogersNickM2103:06:58M20-24 3
WallinChadM2602:31:49M25-29 1
MillerShawnM2602:32:33M25-29 2
YurekJacobM2502:59:33M25-29 3
WestPatrickM3302:45:59M30-34 1
Grafenstein-KinzMicahM3302:53:28M30-34 2
ZiegenfussJoeM3102:59:14M30-34 3
WellsMikeM3902:56:43M35-39 1
RobinJimM3802:57:15M35-39 2
SchatkowskyCraigM3703:02:16M35-39 3

No, you won't find my result in there - it wasn't a good day for me, but if you're trying to quickly do the results for the race management group you can see how the new feature makes life easier.

Allen

Comments