简体   繁体   English

BigQuery SQL:如何在 CASE 表达式中使用不同的列

[英]BigQuery SQL: How to use different columns in CASE expression

Input Data:输入数据:

columnA  columnB
true     false
true     true
false    false
false    true

Problem Statement: From above mentioned data, I want to use different columns to get the result.问题陈述:根据上述数据,我想使用不同的列来获得结果。

Expected Output:预期 Output:

columnA  columnB   result
true     false     A
true     true      B
false    false     C
false    true      C

Tried SQL Query:试过 SQL 查询:

SELECT
columnA, 
columnB,
CASE columnA WHEN 'true' AND columnB ='false' THEN 'A'
             WHEN 'true' AND columnB ='true' THEN 'B'
             ELSE 'C' END AS result

It seems unable to use different columns in CASE expression.似乎无法在 CASE 表达式中使用不同的列。 Is there any solution?有什么解决办法吗?

Yes you can use different columns but, you need to rewrite your query是的,您可以使用不同的列,但是您需要重写查询

SELECT
columnA, 
columnB,
CASE WHEN columnA  = 'true' AND columnB ='false' THEN 'A'
             WHEN columnA  = 'true' AND columnB ='true' THEN 'B'
             ELSE 'C' END AS result
FROM mytable

Consider below "version"考虑下面的“版本”

select *,
  case (columnA, columnB) 
    when (true, false) then 'A'
    when (true, true) then 'B'
    else 'C'
  end result
from your_table    

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM