2

I have a table with a column "version" It specifies software version with a string "3.2.2.0" for example It can have too the "DEBUG" version

I want to obtain the MAX value of the column, but omitting DEBUG, DEBUG only shown if is the only value

Examples:

Version
-------
3.2.0
3.2.0
DEBUG
3.2.1
3.2.2

MAX(version) 
result obtained -> DEBUG
result desired -> 3.2.2

Version
-------
DEBUG
DEBUG
DEBUG

MAX(version) 
result -> DEBUG  OK!

Thanks!

Juan
  • 588
  • 1
  • 8
  • 25

2 Answers2

3
select max(version)
from my_table
where version != 'DEBUG'
union
select max(version)
from my_table
where not exists (select * from my_table where version != 'DEBUG');

Or if you're using mysql:

select ifnull(max(version), 'DEBUG')
from my_table
where version != 'DEBUG'

and for postgres:

select coalesce(max(version), 'DEBUG')
from my_table
where version != 'DEBUG'
Bohemian
  • 412,405
  • 93
  • 575
  • 722
3

This only touches the table once. It is this simple...

...
CASE
   WHEN MIN(Version) = 'DEBUG' THEN 'DEBUG'
   ELSE MAX(NULLIF(Version, 'DEBUG'))
END AS Version
...

Original, before update:

...
MAX(CASE WHEN Version = 'DEBUG' THEN '' ELSE Version END)
...
gbn
  • 422,506
  • 82
  • 585
  • 676
  • @Bohemian: why? Prove it. Edit: MySQL and SQL Server examples on SO: http://stackoverflow.com/q/5143460/27535 – gbn Jun 10 '11 at 11:20
  • 2
    elegant use of of CASE, but it won't ever return 'DEBUG' even in the second case mentioned when DEBUG would be the correct result. – Ray Jun 10 '11 at 11:25
  • @Ray: ah, missed that bit. Will correct. Thanks for explaining what I'd missed, unlike some folk... – gbn Jun 10 '11 at 11:27