Conditional grouping

  • I am trying to get the first row of a grouped query without cursors. The cursor solution but that's taking forever.

    Say I have table T with columns, ID, Title, Price

    select * from T order by Price

    1title1850

    21title1950

    34title21100

    3title11200

    12title21300

    31title11430

    ...

    I need a query to only pick first rows of every group when grouped by Title and sorted by price. So, the query would return ID and Title.

    1title1

    34title2

    Help!

    Yevgeny

  • Hi

    Try this:

    SELECT * FROM

    (SELECT * ,ROW_NUMBER()

    OVER (PARTITION BY title Order BY price) as row_no

    FROM T) x

    WHERE ROW_NO = 1

    Regards

    Alun

  • Thanks, PARTITION BY is a great trick. I should start using it.

Viewing 3 posts - 1 through 2 (of 2 total)

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