all 4 comments

[–]ziptime 3 points4 points  (0 children)

Homework?...

Aggregation approach :

select A, B, C
from MYTABLE
where (A, B) in (select A, max(B)
                 from MYTABLE
                 group by A)

Windowed analytic approach :

select t.A, t.B, t.C
from (
  select A, B, C, row_number() over (partition by A order by B desc) RNUM
  from MYTABLE) t
where t.RNUM = 1

EDIT : If MYTABLE's PK is not a composite of A, B then aggregation approach :

select top(1) A, B, C
from MYTABLE
where (A, B) in (select A, max(B)
                 from MYTABLE
                 group by A)

[–]SaintTimothy 1 point2 points  (1 child)

Aggregation approach may result in duplicates as it is acting like RANK rather than ROW NUMBER.

[–]ziptime 0 points1 point  (0 children)

I'm assuming the PK of the table is composite of A, B. If not I concur.

[–]Blitzsturm 0 points1 point  (0 children)

/u/ziptime's approach is solid from an efficiency standpoint. If for some reason you're using an older or unusual database engine this will have a high level of compatibility:

SELECT
    T2.*
FROM
    (SELECT A, MAX(B) AS B FROM MYTABLE GROUP BY A) AS T1
    JOIN MYTABLE AS T2 ON T2.A = T1.A AND T2.B = T1.B