SQL Server Performance

last row in all categories

Discussion in 'T-SQL Performance Tuning for Developers' started by daidaluus, Jan 5, 2003.

  1. daidaluus New Member

    can anybody help me?

    i have something like this

    id rank qty
    ---- ---- ---
    1 1 10
    1 2 5
    1 3 8
    2 1 16
    2 2 9
    3 1 11
    3 2 20
    3 3 42
    4 1 19

    how can i get the qty for last rank of each id. the result should be like this

    id rank qty
    ---- ---- ---
    1 3 8
    2 2 9
    3 3 42
    4 1 19
  2. Chappy New Member

    try

    select id, rank, qty from MyTable as BaseTable where
    rank = (select MAX(rank) from MyTable as InnerTable where BaseTable.id = InnerTable.id)
    order by id, rank

Share This Page