BigQuery SQL: How to use different columns in CASE expression - sql

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:
columnA columnB result
true false A
true true B
false false C
false true C
Tried SQL Query:
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. 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

Related

Create custom column based off of other columns in SQL query (SQL Server)

I'm having a hard time finding correct syntax to do the following:
SELECT
ColumnA,
ColumnB,
ColumnC,
(if Column1 IS Null and Column2 IS NOT NULL) Then 'Pending' Else '' AS ColumnD
I've tried IF/ELSE, IIF(), but I can't seem to get these queries to work.
use case when expression
SELECT ColumnA,ColumnB,ColumnC,
case when Column1 IS Null and Column2 IS NOT NULL Then 'Pending' Else '' end AS ColumnD
from yourtable

Multiple Columns in Case Statement

Hello I am working on a simple case statement in SQL and was wondering if there is a way to search several columns in the when clause.
Something like:
Case
When (columnA,ColumnB,ColumnC,..,ColumnZ) = 'Something' Then 'Yes'
Else No
End
Where 'Something' is in one of those columns.
I think you want in:
When 'Something' in (columnA, ColumnB, ColumnC,.., ColumnZ) Then 'Yes'
Note that this is an or condition, not that all match.
Yes. You have to check the condition for each column
Case
When (columnA = 'Something' OR ColumnB = 'Something' OR ColumnC = 'Something' .... OR ColumnZ = 'Something') Then 'Yes'
Else 'No'
End

TSQL SELECT CASE & change second column value

I need to change the status of ColumnB depending on the value of ColumnA. Something like pseudocode:
CASE WHEN ColumnA = 'True' THEN ColumnB = 'Alert' ELSE ColumnB
I am using Azure SQL Server.
The pseudo code actually seems quite right. Just drop the assignment to ColumnB and add an end:
SELECT ColumnA,
CASE WHEN ColumnA = 'True' THEN 'Alert' ELSE ColumnB END
FROM MyTable
Also, note you can use a slightly neater syntax (although it's a matter of taste, mostly), since all (of the one) conditions you have are on the same expression:
SELECT ColumnA,
CASE ColumnA WHEN 'True' THEN 'Alert' ELSE ColumnB END
FROM MyTable
Based on your provided example, I don't think you even need a case statement. A simple update will work just fine.
update YourTable
set ColumnB = 'Alert'
where ColumnA = 'True'
If, on the other hand, you are actually updating other columns at the same time based on other conditions, which prevents you from inserting the where ColumnA = 'True' clause, then you can do it with the case statement this way:
update YourTable
set ColumnB = case when ColumnA = 'True' then 'Alert' else ColumnB end,
ColumnX = ...
from YourTable

SQL Server : case statement

I have table named boolean which contain 'true' and/or 'false' values as strings in one column.
I have problem to create case statement to show me whether there are only 'true' or 'false' or 'both' values
Example 1:
'true'
'true'
result:'true'
Example 2:
'false'
'false'
'false'
Result: 'false'
Example 3:
'true'
'false'
'true'
Result: 'both'
Edit:
case statement should look like:
case
when "column content are only true values" then 'true'
when "column content are only false values" then 'false'
else 'both'
end
You could aggregate the max and min of the column, and then evaluate the results - if they are the same, there's only one value in the column. If not, there must be both. Note that since these are string representations the values are sorted lexicographically:
SELECT CASE WHEN MAX(col) = MIN(col) THEN MAX(col) ELSE 'both' END
FROM my_table
SELECT CASE WHEN MIN(Col) <> MAX(Col) THEN
'Both'
ELSE
MIN(Col)
END
FROM YourTable
select case when count(distinct bool_column) = 2 then 'both'
when sum(case when bool_column = 'false' then 1 end) > 0 then 'false'
else 'true'
end as result
from your_table

Select two columns based on the same conditional statement SQL

I have a select statement which resembles something like this
SELECT
CASE WHEN <SOME-CONDN_1> THEN 'value1' ELSE '' ||
CASE WHEN <SOME-CONDN_2> THEN 'value2' ELSE '' ||
CASE WHEN <SOME-CONDN_3> THEN 'value3' ELSE '' AS value_column,
CASE WHEN <SOME-CONDN_1> THEN 'name1' ELSE '' ||
CASE WHEN <SOME-CONDN_2> THEN 'name2' ELSE '' ||
CASE WHEN <SOME-CONDN_3> THEN 'name3' ELSE '' AS name_column
FROM data_table
--<REST OF THE QUERY>
The conditional statement is something like data_table.data_column ILIKE value1 and so on.
Since I'm doing the same conditioning statement twice (and it involves some string matching using ILIKE) I was wondering if I could club them and make it more efficient.
Would the same be possible using SQL statements?
Option 1: not quite sure if this variant of CASE works for PostgreSQL...
select case cond_num when
1 then 'value1'
when 2 then 'value2',
when 3 then 'value3' else null end as value_column,
case cond_num when
1 then 'name1'
when 2 then 'name2',
when 3 then 'name3' else null end as name_column
from (
select data_table.*,
case when <some_condition_1> then 1
when <some_condition_2> then 2
when <some_condition_3> then 3 else 0 end as cond_num
from data_table
) screened_table
;
Option 2:
select case when
cond1 = 1 then 'value1'
when cond2 = 1 then 'value2',
when cond3 = 1 then 'value3' else null end as value_column,
case when
cond1 = 1 then 'name1'
when cond2 = 1 then 'name2',
when cond3 = 1 then 'name3' else null end as name_column
from (
select data_table.*,
case when <some_condition_1> then 1 else 0 as cond1,
case when <some_condition_2> then 1 else 0 as cond2,
case when <some_condition_3> then 1 else 0 as cond3
from data_table
) screened_table
;
Option 3 - note if the conditions are not exclusive may return multiple rows. Will not return rows from data_table in which no conditions are true.
select rslt.name, rslt.value
from data_table, (
select 1 as cond, 'value1' as value, 'name1' as name
union all
select 2 as cond, 'value2' as value, 'name2' as name
union all
select 3 as cond, 'value3' as value, 'name3' as name
) rslt
WHERE (<some_condition_1> and rslt.cond = 1) OR
(<some_condition_2> and rslt.cond = 2) OR
(<some_condition_3> and rslt.cond = 3)
;
Assuming the results are both strings you can use an array to make things simpler.
SELECT a[1],a[2], ...
FROM (SELECT CASE
WHEN <SOME-CONDN_1> THEN ARRAY['value1','name1']
WHEN <SOME-CONDN_2> THEN ARRAY['value2','name2']
WHEN <SOME-CONDN_3> THEN ARRAY['value3','name3']
ELSE '' AS a
FROM ...
);
If the result values are not all the same type you can do the same thing using a ROW() constructor, but you will need to define a type in order to get the values individually "back out of the row".