How to use Postgres CASE simple/short-hand syntax with multiple conditions? - sql

I know with Postgres CASE expression, you can simplify it down to:
SELECT a,
CASE a WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
ELSE 'other'
END
FROM test;
...so you don't have to write etc...
CASE
WHEN a = 1 THEN ''
WHEN a = 2 THEN ''
.
..
...
WHEN a = 99 or a = 100 THEN ''
ELSE '' END
But is there a way to do this on multiple conditions with a keyword like ILIKE or LIKE? i.e.
SELECT a,
CASE a WHEN LIKE '1' or LIKE 'one' THEN 'one'
WHEN LIKE '2' and (LIKE 'two' or LIKE 'too') THEN 'two'
ELSE 'other'
END
FROM test;
Obviously this above doesn't work and I was trying some other variations but could not get it to work (if its possible)?

No, the short CASE syntax only works for a single condition per branch, and the comparison must be with the = operator. Use the other syntax for what you want.

You could use Postgres' regex operator here:
SELECT a,
CASE WHEN a ~ '^(1|one)$' THEN 'one'
WHEN a ~ '^(2|two)$' THEN 'two'
ELSE 'other'
END
FROM test;

Related

to write case when to another case when in one select Google Big Query

I'm a new user in Google Big Query and I want to write two "Case when...." in one select, and they should be connected to each other
For example:
Case when number = 1 then 'one' when number=2 then 'two' else 'other' end as mark
Case when mark = 'one' then 'red' when mark='two' then 'orange' else 'other' end as color
But I have mistake like 'Unrecognized name: mark at [8:14]'
I used all the options with apostrophe but still have the same result.
I also try to find answer in BigQuery Documentation but it was unsuccessfully.
Is it even possible to do this?
I assume you're using Google BigQuery's current SQL syntax support, as opposed to their older SQL implementation.
SQL does not allow column expressions to refer to other column aliases in the same level, so you will need to use an outer-query. Yes, it's dumb, blame the ISO SQL committee for creating such a terribly unergonomic language.
You'll want something like this:
I'm not a BigQuery user so I don't know if this syntax is fully supported, but I believe the query below is ISO SQL-compliant).
Note the use of CASE <expr> WHEN <value> instead of CASE WHEN <expr> = <value> btw.
SELECT
t.*
t.number,
CASE t.mark
WHEN 'one' THEN 'red'
WHEN 'two' THEN 'orange' ELSE 'other'
END AS color
FROM
(
SELECT
y.*,
CASE y.number
WHEN 1 THEN 'one'
WHEN 2 THEN 'two' ELSE 'other'
END AS mark
FROM
someTable AS y
) AS t
Another alternative is to use a CTE, which is essentially the same as the above but the inner-query is expressed above the outer-query instead of inside it (and it can be reused more easily in the same query):
WITH t AS (
SELECT
y.*,
CASE y.number
WHEN 1 THEN 'one'
WHEN 2 THEN 'two' ELSE 'other'
END AS mark
FROM
someTable AS y
)
SELECT
t.*
t.number,
CASE t.mark
WHEN 'one' THEN 'red'
WHEN 'two' THEN 'orange' ELSE 'other'
END AS color
FROM
t;
Consider also below option (BigQuery)
select *,
Case mark when 'one' then 'red' when 'two' then 'orange' else 'other' end as color
from data, unnest([
struct(Case number when 1 then 'one' when 2 then 'two' else 'other' end as mark)
])
Obviously this is a matter of preferences, but in my practice, I am using above approach much more frequently than subquery and/or cte
You can test it using dummy data as in below example
with data as (
select 1 number union all
select 2 union all
select 3
)
select *,
Case mark when 'one' then 'red' when 'two' then 'orange' else 'other' end as color
from data, unnest([
struct(Case number when 1 then 'one' when 2 then 'two' else 'other' end as mark)
])
with output

SQL Server - Case Statement

I'm almost certain you cannot do this within the context of the case statement, and I haven't been able to find any documentation about it, but is it possible to do the following:
SELECT CASE WHEN testValue > 2
THEN testValue(Without Repeating it) ELSE FailValue)
END
FROM Table
A better more thorough example:
Select CASE WHEN (Foo-stuff+bar) > 2
THEN Conditional statement without >2 Else "Fail"
END
FROM TABLE
I am looking for a way to create a select without repeating the conditional query.
EDIT: Due to a poor example on my part, and the lack of answers I was looking for:
testValue = (Table.A / Table.B) * Table.C Table.D
SELECT CASE WHEN testValue > 2
THEN testValue ELSE FailValue)
END
FROM Table
Like so
DECLARE #t INT=1
SELECT CASE
WHEN #t>0 THEN
CASE
WHEN #t=1 THEN 'one'
ELSE 'not one'
END
ELSE 'less than one'
END
EDIT:
After looking more at the question, I think the best option is to create a function that calculates the value. That way, if you end up having multiple places where the calculation needs done, you only have one point to maintain the logic.
The query can be written slightly simpler, like this:
DECLARE #T INT = 2
SELECT CASE
WHEN #T < 1 THEN 'less than one'
WHEN #T = 1 THEN 'one'
ELSE 'greater than one'
END T
I am looking for a way to create a select without repeating the conditional query.
I'm assuming that you don't want to repeat Foo-stuff+bar. You could put your calculation into a derived table:
SELECT CASE WHEN a.TestValue > 2 THEN a.TestValue ELSE 'Fail' END
FROM (SELECT (Foo-stuff+bar) AS TestValue FROM MyTable) AS a
A common table expression would work just as well:
WITH a AS (SELECT (Foo-stuff+bar) AS TestValue FROM MyTable)
SELECT CASE WHEN a.TestValue > 2 THEN a.TestValue ELSE 'Fail' END
FROM a
Also, each part of your switch should return the same datatype, so you may have to cast one or more cases.
We can use case statement Like this
select Name,EmailId,gender=case
when gender='M' then 'F'
when gender='F' then 'M'
end
from [dbo].[Employees]
WE can also it as follow.
select Name,EmailId,case gender
when 'M' then 'F'
when 'F' then 'M'
end
from [dbo].[Employees]

SQL case with different fields to check from same table

I have the following problem:
I have a select statement that includes a case part. Up til there it is easy the problem is that the case includes a check against another field in the same table.
select h.id,
case h.value
when 'P' then 'test'
when '' then 'failed'
when 'D' then 'passed'
else null end
as info,
b.text,
case h.diag
when h.value = '' [or 'failed' not sure tried both and didn't work]
else h.diag end
as diag1, h.date from valuetab h, texttab b where h.id=b.id
I want to have h.diag only to show values when h.value is not failed.
I always get the mistake that the = should be concat.. but that doesn't make sense in my eyes.
Any ideas??
Thats for all your help.
You can also write a case statement with your expression in a different place i.e.
SELECT CASE WHEN X = 1 THEN 'Y' WHEN X = 2 THEN 'Z'
I think what you want to do is something more like this:
SELECT CASE WHEN h.value = '' THEN h.diag end
Use the other form of case statement, which doesn't specify a column you want to look at:
select case
when column1 = 2 then 'Foo'
when other_column = 'blah' then 'Bar'
end
from table
The problem with using case column1 when... is that it implicitly compares column1 to each when clause. You can't then include a comparison to some other column in it.
You are missing a THEN portion of the WHEN clause, and specifying a condition where you could specify a value:
case h.value
when '' THEN NULL
else h.diag end
Ok got it....
after the 2nd case the "h.diag" must be removed....
so it is
case
when h.value = '' then null
else h.diag end
as diag1,

Quick SQL Syntax Question

I have a standard insert into / select statement that is formatted similar to this:
insert into Table
( ...,
...)
select
field1,
field2,
field3,
field4,
fieldetc
from .... etc
There are three specific fields in the select statement that will need different values selected depending on another field, let's call it checker and the three fields are field2, field3, and field 4. The values will either be a 0 or in the other situation will need a case when. My question is, how do I format an if/else statement so it will work within the select statement? How I have it now is like this:
select
field1data,
if checker = 'A' or checker is null
begin
0,
0,
0,
end
else
begin
case when ... then ... else ... end,
case when ... then ... else ... end,
case when ... then ... else ... end,
end
fieldetcdata
from... etc
This is returning errors. How can I format this so it will work correctly, either selecting zeroes for these three fields or running through my case when statements in the other situation. Thanks!
You'll need to specify case statement for each field separately.
Select field1data,
Case When IsNull(Checker,'A') = 'A' Then 0
When Cond1 Then Whatever1
...
Else ...
End,
Case When IsNull(Checker,'A') = 'A' Then 0
When Cond2 Then Whatever1
...
Else ...
End,
Case When IsNull(Checker,'A') = 'A' Then 0
When Cond2 Then Whatever1
...
ELSE ...
End,
fieldetcdata
From ETC
Take out the IF and the BEGIN/END stuff and it should work. All you need to use is the
CASE COALESCE(checker,'A') WHEN 'A' THEN 0 ELSE alternate_value END
for each conditional value you want to SELECT
EDIT: Using your example:
SELECT
field1data,
CASE WHEN ISNULL(checker) THEN alternate_value1
WHEN checker = 'B' THEN alternate_value11 END,
CASE WHEN ISNULL(checker) THEN alternate_value2
WHEN checker = 'B' THEN alternate_value22 END,
CASE WHEN ISNULL(checker) THEN alternate_value3
WHEN checker = 'B' THEN alternate_value3 END,
fieldetcdata
FROM
TABLE
EDIT2: For multiple conditions, you simply add WHEN clauses.
http://msdn.microsoft.com/en-us/library/ms181765.aspx
Edited based on comments below.
You need to use a CASE statement with multiple WHEN conditions.
SELECT
field1data,
CASE COALESCE(checker, 'A') WHEN 'A' THEN 0 WHEN condition2 THEN ... ELSE ... END,
CASE COALESCE(checker, 'A') WHEN 'A' THEN 0 WHEN condition2 THEN ... ELSE ... END,
CASE COALESCE(checker, 'A') WHEN 'A' THEN 0 WHEN condition2 THEN ... ELSE ... END,
CASE COALESCE(checker, 'A') WHEN 'A' THEN 0 WHEN condition2 THEN ... ELSE ... END,
CASE COALESCE(checker, 'A') WHEN 'A' THEN 0 WHEN condition2 THEN ... ELSE ... END
FROM ...

Returning custom values based on query results

Is it possible to make a query that returns a different custom value based on a query? Here is an example:
What I have in the table ...
number
1
2
3
... and this is what I want returned:
number
one
two
three
How can this be done?
You want a case statement. Depending on your flavor of SQL, something like this should work:
select
bar = case
when foo = 1 then 'one'
when foo = 2 then 'two'
else 'baz'
end
from myTable
Try
select value = case t.value
when 1 then 'one'
when 2 then 'two'
when 3 then 'three'
...
else null
end
from my_table t