Maximum Value across columns with column names in PostgreSQL

PHOTO EMBED

Wed Sep 14 2022 05:57:26 GMT+0000 (Coordinated Universal Time)

Saved by @ClemensBerteld #postgres #sql #maximum #greatest

select 
  case greatest(col1,col2,col3,col4) 
    when col1 then 'col1:' || col1
    when col2 then 'col2:' || col2
    when col3 then 'col3:' || col3
    when col4 then 'col4:' || col4
    else null
  end as greatestcolumnname
from mytable;
content_copyCOPY