google bigquery - Get MAX from row with column name (SQL) -


sorry if questin simple, spent 1 day googling , still can't figure out how solve this:

i have table like:

userid b c d e   1    5 0 2 3 2   2    3 2 0 7 3 

and need each max per row column name:

userid max   1       2     d 

all ideas appreciated! thanks! use google big query possibilities different form mysql understand, try translate if have ideas in mysql way.

you can use greatest:

select userid, case greatest(a,b,c,d,e)                     when 'a'                     when b 'b'                     when c 'c'                     when d 'd'                     when e 'e'                end max tablename 

result:

userid  max 1       2       d 

with sample table looks like:

select userid, case greatest(a,b,c,d,e)                      when 'a'                                             when b 'b'                     when c 'c'                     when d 'd'                     when e 'e'                end max (select  1 userid,   5 a, 0 b, 2  c, 3 d, 2 e), (select  2 userid,   3 a, 2 b, 0  c, 7 d, 3 e) 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -