Ramon Wenzel
Ramon Wenzel

Reputation: 69

Use SQL to recode column value

Questionnaire data (ordinal: 1,2,3,4 or 5) in Google BigQuery need to be fetched and aggregated using SQL syntax sum() or avg(). Before such column values aggregation can take place, some ordinal values (i.e., columns) need to be 'recoded', e.g.:

An example to explain the situation: the original data collection involved responding to:

How do I do that?

Upvotes: 0

Views: 2005

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270401

If those are the only values and you want to sum them, you can use:

sum(6 - item2)

Upvotes: 1

GMB
GMB

Reputation: 222582

If I understand correctly, you can use a case expression:

select sum(case item2
    when 1 then 5
    when 2 then 4
    when 3 then 3
    when 4 then 2
    when 5 then 1
end) sum_item2
from mytable

Upvotes: 0

Related Questions