Returning custom values based on query results - sql

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

Related

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

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;

Is there a way to create a new column and assign values based on criteria on an existing column in a SQL query?

I want to create a new column in my query output and assign values to it based on existing columns in my input. Let's say i have column A in my input and want to create column B in my query output. How do I add the following logic in a sql select statement?
If A = 1 then set B to x
Else if A = 2 then set B to y
Else if A = 3 then set B to z
Else set B to null
I have to do this for multiple new columns in my output within the same query.
This is a case expression:
select . . .,
(case A when 1 then 'x' when 2 then 'y' when 3 then 'z' end) as b
Something like this?
SELECT A,
CASE
WHEN A = 1 THEN 'x'
WHEN A = 2 THEN 'y'
ELSE 'z'
END AS B
FROM ...

SQL AS query results in duplicate column

I have a SQL query like so
SELECT
name,
CASE WHEN (new_value=2) THEN 0 END as out,
CASE WHEN (previous_value=2) THEN 1 END as out
FROM my_table;
This results in duplicate columns:
name out out
foo 1 null
bar null 1
instead of
name out
foo 1
bar 0
How do I fix this?
You want one case expression with two conditions:
SELECT name,
(CASE WHEN new_value = 2 THEN 0
WHEN previous_value = 2 THEN 1
END) as out
FROM my_table;
Consider:
SELECT
name,
CASE
WHEN new_value = 2 THEN 0
WHEN previous_value = 2 THEN 1
END as out
FROM my_table;
In your query, each case expression generates one column in the resulset. You want only one, with two branches (denoted by when ... then ...)
You are getting null output, so you need to add else on this.
select name,
case
when new_value = 2 then 0
when previous_value = 2 then 1
else 0 end as out
from my_table;

Identify last record in CASE

Probably an easy one... I have a SQL query along the lines...
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM test;
(from the docs)
I want to identify the last record in the set and act on that condition.
There are two possible scenarios from what you have explained in your question.
One of them is the one in which for the max value found in column a you want to display a certain message:
SELECT
a
, CASE
WHEN a = 1 THEN 'ONE'
WHEN a = 2 THEN 'TWO'
WHEN a = (SELECT MAX(a) FROM test) THEN 'MAX'
ELSE 'OTHER'
END
FROM TEST;
The other possible scenario is that only for the last record in the table you want to display that certain message. And in that scenario your query needs to change to:
SELECT
a
, CASE
WHEN a = 1 THEN 'ONE'
WHEN a = 2 THEN 'TWO'
WHEN a = (SELECT TOP 1 a FROM TEST ORDER BY a DESC) THEN 'MAX'
ELSE 'OTHER'
END
FROM TEST
ORDER BY A;

SQL, Select if or select case

I am not sure what I am trying is achievable or not!!
I am trying to write a SQL query will will do select statement based on user input.
so if user input = 1 then I want it to select from actual table.
if user input = 0 then I want it do select 0 or null from dual. (if this is possible).
so Here is Parameter which will used to get input from user. ?i_userkey:'':null?
if user input's 1 then it will change null to 1.
I want to write a query using this parameter. something like this.
below is the logic.
IF i_userkey = 1 then
select ID,Gender,Age from TableA
If i_userkey = 0 then
select 0 or null from dual.
is this possible?
How about this:
SELECT
CASE WHEN i_userkey = 1 THEN ID ELSE NULL END AS ID
CASE WHEN i_userkey = 1 THEN Gender ELSE NULL END AS Gender
CASE WHEN i_userkey = 1 THEN AGE ELSE NULL END AS Age
FROM TableA
This will at least give you a consistent three-column result set you can work with. Having the query return differing column counts is not going to work.
select ID,Gender,Age
from TableA
where i_userkey = 1
union all
select 0, 0, 0
from dual
where i_userkey = 0
You might have to adjust the datatypes in the dual-select to match TableA