I have an aggregate function that does a group by (col A). It selects the maximum value from a set of columns(col B), but I also want to return another value from a column
This is a very common problem - "show me other columns on the rows matching my min()/max() aggregate criteria." On large tables, subquery strategies can become very slow, and ranking functions are sometimes not much better.
If you're willing to get your head around it, this is by far the most performant way to handle this (though again, not the most readable):
SELECT A, cast(left(val, 8) as int) AS B, substring(val, 9, 999) AS C
FROM ( SELECT A, max(str(B, 8) + C) AS val FROM myTable GROUP BY A) t
You can concatenate anything you want to what you're max
ing, then extract it in the outer query. Voilá.
Note that this will return different results than the solutions posted by bluefeet and JW, in that if there are multiple matching max values per group, this method will pick a winner (the largest C) whereas the others will return multiple records. So, if the 3rd B value were 100 instead of 125, this will return 1, 100, dae whereas the other solutions would return both 1, 100, abd and 1, 100, dae.