Skip to content
Advertisement

Replace and squeeze boolean feedback columns to single score column in BigQuery

I have data that looks like this (note one TRUE per row):

I want to replace each TRUE with a number that depends on the column that it is in (5-1), so a score with very_good being 5 and very_poor being 1, and then squeeze it into one column. So it looks like this:

So far I’ve tried this:

But this creates multiple columns and I can’t find a way to do multiple REPLACE’s within a single column, additionally, this doesn’t change the FALSE which needs to be removed. Ideally, I need to deal with some rows being filled with null values as well.

Advertisement

Answer

You can use a case expression:

Note that for this to make sense, only one column in each row should be true – otherwise, the case expression returns the highest scoring column.

The following might also work in Big Query, if the columns are of boolean datatype:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement