My query is supposedly return the COLUMN NAME based on the maximum values of the columns. I tried using CASE WHEN to solve this case. but somehow this error occur, probably because I return the name of column instead of the value of the column itself:
No matching signature for operator CASE; all THEN/ELSE arguments must be coercible to a common type but found: INT64, STRING; actual argument types (WHEN THEN) ELSE: (BOOL STRING) (BOOL INT64) INT64 at [3:5]
My code is:
SELECT
ID,
CASE
WHEN col1 >= col2 AND col1 >= col3 AND col1 >= col4 AND col1 >= col5 THEN 'col1 '
WHEN col2 >= col1 AND col2 >= col3 AND col2 >= col4 AND col2 >= col5 THEN 'col2 '
ELSE 'col1'
END AS Max_Column_Name
FROM table
Sample input is:
Is there any way to make this query more simple? So no need to repeat the when..case if there are lots of column. Other initiative is to use GREATEST(col1,col2,col3) like in this post but, Im not sure how to use it in standard sql, bigquery.
Expected output:
Return the name of column that has maximum value for each segment (country,product,language) and rename the name of column in the table result.