0

Basically I have a sample table:

Town          V1     V2      V3      V4      V5
New York    0.12    0.1    0.11     0.21    -0.12

From a sample query:

SELECT town, v1, v2, v3, v4, v5 FROM sample

I would like a query to return just the following:

New York V4 0.21

Which means, I would like to return town, header of the max value (V4) and max value (0.21)

Is there any easy way of doing it in MySQL?

Darius
  • 43
  • 4

1 Answers1

0

If you have no NULL values, then:

select town, greatest(v1, v2, v3, v4, v5)

This is more painful if you do have NULL values. If you know the values are never negative:

select town,
       nullif(greatest(coalesce(v1, -1), coalesce(v2, -1), coalesce(v3, -1),
                       coalesce(v4, -1), coalesce(v5, -1)), -1)
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786