我的下表如下:
id | gained | init
-----------------------
1da | 56 | TRUE
1da | 56 | FALSE
e4f | 23 | TRUE
few | 23 | TRUE
新列new_gained应通过查询派生,逻辑应为:
CASE WHEN init is true THEN 0 ELSE gained END
我想要的查询结果:
id | gained | init | new_gained
-----------------------------------
1da | 56 | TRUE | 0
1da | 56 | FALSE | 56
e4f | 23 | TRUE | 0
few | 23 | TRUE | 0
答案 0 :(得分:1)
不确定问题的根源是什么-无论如何-以下是您的问题
#standardSQL
WITH `project.dataset.table` AS (
SELECT '1da' id, 56 gained, TRUE init UNION ALL
SELECT '1da', 56, FALSE UNION ALL
SELECT 'e4f', 23, TRUE UNION ALL
SELECT 'few', 23, TRUE
)
SELECT *,
CASE WHEN init THEN 0 ELSE gained END AS new_gained
FROM `project.dataset.table`
另一个选择是
#standardSQL
SELECT *,
IF(init, 0, gained) AS new_gained
FROM `project.dataset.table`