Displaying rows - basic sort problem?

  • I have done the following sort:

    SELECT Sex, Distance, Time

    FROM RaceResults

    ORDER BY Sex, Distance, Time

    To get:

    SexDistanceTime

    F1.60900:08:08

    F1.60900:08:23

    F3.86100:19:26

    F3.86100:19:28

    F3.86100:20:14

    However, I only want to display in my view the Top result for Each distance (the fastest time). I tried SELECT TOP 1 but this just shows the first row, whereas, I want to view the first row for Each distance.

    Please help this is driving me insane!!!

  • Try this:

    SELECT Sex, Distance, MIN(Time) AS Time

    FROM RaceResults

    GROUP BY Sex, Distance

    ORDER BY Sex, Distance

     

  • The first line creates a problem:

    "The select statement includes a reserved word or an argument that is misspelled or missing, or the punctuation is incorrect"

    I am not sure whats wrong...??

  • oh wow it is so close now please help i beg!! haha

    Its the last word on the first line Time it doesnt like:

    SELECT Sex, Distance, MIN(Time) AS Time

    With:

    SELECT Sex, Distance, MIN(Time) AS Expr1

    It works but the whole column just has the word Time in it. Weird!

  • Not sure what is happening with your query, other than the issue that "Time" is a reserved word.  Try this: 

    DECLARE @RaceResults TABLE( Sex char(1),

                                               Distance numeric(5,3),

                                               [Time] varchar(8))

    INSERT INTO @RaceResults

    SELECT 'F', 1.609, '00:08:23' UNION ALL

    SELECT 'F', 1.609, '00:08:08' UNION ALL

    SELECT 'F', 3.861, '00:19:28' UNION ALL

    SELECT 'F', 3.861, '00:19:26' UNION ALL

    SELECT 'F', 3.861, '00:20:14'

    SELECT Sex, Distance, MIN( [Time]) AS [Time]

    FROM @RaceResults

    GROUP BY Sex, Distance

    ORDER BY Sex, Distance

     

    I wasn't born stupid - I had to study.

  • Hi

    I got around the problem by adding another time column to the table and used Min(Time) again and bingo it worked! Thanks!

    SELECT Sex, Distance, MIN( [Time]) AS [Time], MIN('TIME2') AS Exp2

  • Hello again,

    How could I insert the ID column without it affecting the sort?

    Thanks,

    Bigtimeconfused

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply