I’m quite new user of big query with basic SQL skills. I would like to build a query that is using a conditional IF statement to add a new value in one of my field
Here is the data that i have
x
YEAR | MONTH | NEW YEAR
----------+----------+-------------
2018 | April |
2019 | June |
2020 | May |
I was thinking about the following request :
Select
IF (YEAR ="2018" AND MONTH=April) THEN (NEW YEAR =2019),
IF (YEAR ="2019" AND MONTH=June) THEN (NEW YEAR =2020),
IF (YEAR ="2020" AND MONTH=May) THEN (NEW YEAR =2021)
FROM 'database'
WHERE source ='name source'
i’m trying to figure out, but impossible to set the right syntax and logic
Many thanks for your help Much appreciated !
Advertisement
Answer
Use a case
expression:
SELECT (case when YEAR = '2018' and month = 'April' then 2019
when YEAR = '2019' and month = 'June' then 2020
when YEAR = '2020' and month = 'May' then 2021
end) as NEW YEAR
FROM `database`
WHERE source = 'name source'