how to group by column name and get max value and its zone?

  • how to group by following column and get the required output shown below?

    name value zone

    CA129R2748East

    CA129R543west

    CA129R3000north

    CA129R100south

    ....

    required output:

    name maxvalue zone

    CA129R 3000 north

    Thanks in advance!

  • It's not that hard, it's a matter of doing a sub-query as a joined table getting the MAX() for each of the primary items, then rejoining to the primary table.

    IE: select a.* from tbl a JOIN (SELECT [name], max(value) as maxvalue from tbl group by name) as b on a.name = b.name and a.value = b.maxvalue.

    If you create consumable data/schema as described in the first link in my signature you'll get some testable code instead of just a quick example.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

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

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