Select statement inside CASE - SQL - sql

I am trying to figure out the right code for the below logic. If there is a certain value in the first column, display the value from the second column for that specific record.
Can someone please help? Thanks.
CASE WHEN TableA.Column1 = 'a' THEN 'select TableA.Column2 '
WHEN TableA.Column4 = 'b' THEN 'select TableA.Column5'
ELSE TableA.Column6
END AS [Test]

You were almost there just remove the select from case statement. Since all the values are coming from same table no need of select in case statement just keep column name it will fetch the corresponding column value.
SELECT CASE
WHEN Column1 = 'a' THEN Column2
WHEN Column4 = 'b' THEN Column5
ELSE Column6
END AS [Test]
FROM tableA

Try this:
SELECT (CASE WHEN A.Column1 = 'a' THEN A.Column2
WHEN A.Column4 = 'b' THEN A.Column5
ELSE A.Column6
END) AS [Test]
FROM TableA A;

You are already inside a SELECT, so you do not need an additional SELECT inside:
SELECT
...
, CASE WHEN TableA.Column1 = 'a' THEN TableA.Column2
WHEN TableA.Column4 = 'b' THEN TableA.Column5
ELSE TableA.Column6
END AS [Test]
FROM
TableA
...

You could write it as:
select CASE WHEN TableA.Column1 = 'a' THEN TableA.Column2
WHEN TableA.Column4 = 'b' THEN TableA.Column5
ELSE TableA.Column6
END AS [Test]
from tableA
or even shorter in your case:
select CASE TableA.Column1
WHEN 'a' THEN TableA.Column2
WHEN 'b' THEN TableA.Column5
ELSE TableA.Column6
END AS [Test]
from tableA

Related

Check existence of a value in a partition using window function in oracle sql

I am having below set of data.
My goal is to have last column Exists with value YES when value C101808491229 exists in the partition (PARTITION BY COLUMN2), else with value NO
I was tried to do so in below two ways :
1.COUNT(CASE WHEN COLUMN5 = 'C101808491229' THEN 'YES' ELSE 'NO' END) OVER (PARTITION BY COLUMN2) AS EXISTS
2.CASE WHEN COLUMN5 = 'C101808491229' THEN 'YES' ELSE 'NO' END AS EXISTS
But neither seemed to work for me.
I hope I made my point clear. Can someone let me know if it is possible to achieve this?
You can simply use the MAX as follows:
MAX(CASE WHEN COLUMN5 = 'C101808491229' THEN 'YES' ELSE 'NO' END)
OVER (PARTITION BY COLUMN2) AS EXISTS
If C101808491229 exists then it will return YES and max will always give YES if there is even one YES and other NO exists in the partition.
Use this version:
CASE WHEN COUNT(CASE WHEN COLUMN5 = 'C101808491229' THEN 1 END)
OVER (PARTITION BY COLUMN2) > 0 THEN 'YES' ELSE 'NO' END AS "EXISTS"
The inner CASE expression does a conditional count per COLUMN2 partition of how many times COLUMN5 has the target value. The outer CASE expression displays YES when that count be greater than zero, otherwise it displays NO.
Note: EXISTS is a keyword, please use some other name for your alias.
You can do it without window function like following.
SELECT YT.*
,CASE
WHEN C.COLUMN2 IS NULL
THEN 'No'
ELSE 'Yes'
END AS "EXISTS"
FROM YOURTABLENAME YT
LEFT JOIN (
SELECT COLUMN2
FROM YOURTABLENAME
WHERE COLUMN5 = 'C101808491229'
) C ON YT.COLUMN2 = C.COLUMN2

simplify multiple union in sql

i am trying to simply the following union query .Basically i am trying to get all the possible values from same table different column and i have to take value which are not equal to no then replace them with specific text when they are from respective column.
select 'a' from Mytable where a!='no' and id='1'
union
select 'b' from Mytable where b!='no' and id='1'
union select 'c' from Mytable where c!='no' and id='1'
so my table structure will be
id Acolumn BColumn Ccolumn
1 123a no 345v
so my expected result is
a c
so please suggest me to simplify this query thanks in advance
According to what you've written, this might be OK.
select case when a <> 'no' and id = '1' then 'a'
when b <> 'no' and id = '1' then 'b'
when c <> 'no' and id = '1' then 'c'
end
from mytable
where (a <> 'no' and id = '1')
or (b <> 'no' and id = '1')
or (c <> 'no' and id = '1')

ORACLE: USE RESULT OF CASE-WHEN-STATEMENT

I have a huge query and I am wondering if it is in Oracle possible
to get the result of a case-when-statement and use it for comparison? My CASE-STATEMENT is declared in the Select-Statement and it looks like this.
SELECT........
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST;
Now I want to get the result of this case-statement and use it in the where part? Is it possible? (Sry this may be a dumb question)
If you define your CASE statement in either an inline-view or a common table expression (aka WITH clause), you can refer to it by whatever alias you give it.
For example (inline-view):
SELECT ...
FROM ( SELECT .....
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST
FROM...
) v
WHERE v.test = 'TEST2';
As a common table expression, it would be:
WITH cte AS ( SELECT........
(CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END) AS TEST
FROM ... )
SELECT ...
FROM cte
WHERE test = 'TEST2';
You can use a case statement in the where clause, for eg.:
select * from table
where table.field = (CASE
WHEN (Select 1 from DUAL) = 1 THEN 'TEST'
ELSE 'TEST2'
END)
This will compare the value returned from the case statement with the table field.

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".

SQL: Alias Column Name for Use in CASE Statement

Is it possible to alias a column name and then use that in a CASE statement? For example,
SELECT col1 as a, CASE WHEN a = 'test' THEN 'yes' END as value FROM table;
I am trying to alias the column because actually my CASE statement would be generated programmatically, and I want the column that the case statement uses to be specified in the SQL instead of having to pass another parameter to the program.
This:
SELECT col1 as a,
CASE WHEN a = 'test' THEN 'yes' END as value
FROM table;
...will not work. This will:
SELECT CASE WHEN a = 'test' THEN 'yes' END as value
FROM (SELECT col1 AS a
FROM TABLE)
Why you wouldn't use:
SELECT t.col1 as a,
CASE WHEN t.col1 = 'test' THEN 'yes' END as value
FROM TABLE t;
...I don't know.
I think that MySql and MsSql won't allow this because they will try to find all columns in the CASE clause as columns of the tables in the WHERE clause.
I don't know what DBMS you are talking about, but I guess you could do something like this in any DBMS:
SELECT *, CASE WHEN a = 'test' THEN 'yes' END as value FROM (
SELECT col1 as a FROM table
) q
#OMG Ponies - One of my reasons of not using the following code
SELECT t.col1 as a,
CASE WHEN t.col1 = 'test' THEN 'yes' END as value
FROM TABLE t;
can be that the t.col1 is not an actual column in the table. For example, it can be a value from a XML column like
Select XMLColumnName.value('(XMLPathOfTag)[1]', 'varchar(max)')
as XMLTagAlias from Table
It should work. Try this
Select * from
(select col1, col2, case when 1=1 then 'ok' end as alias_col
from table)
as tmp_table
order by
case when #sortBy = 1 then tmp_table.alias_col end asc
I use CTEs to help compose complicated SQL queries but not all RDBMS' support them. You can think of them as query scope views. Here is an example in t-sql on SQL server.
With localView1 as (
select c1,
c2,
c3,
c4,
((c2-c4)*(3))+c1 as "complex"
from realTable1)
, localView2 as (
select case complex WHEN 0 THEN 'Empty' ELSE 'Not Empty' end as formula1,
complex * complex as formula2
from localView1)
select *
from localView2
Nor in MsSql
SELECT col1 AS o, e = CASE WHEN o < GETDATE() THEN o ELSE GETDATE() END
FROM Table1
Returns:
Msg 207, Level 16, State 3, Line 1
Invalid column name 'o'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'o'.
However if I change to CASE WHEN col1... THEN col1 it works
If you write only equal condition just:
Select Case columns1 When 0 then 'Value1'
when 1 then 'Value2' else 'Unknown' End
If you want to write greater , Less then or equal you must do like this:
Select Case When [ColumnsName] >0 then 'value1' When [ColumnsName]=0 Or [ColumnsName]<0 then
'value2'
Else
'Unkownvalue' End
From tablename
Thanks
Mr.Buntha Khin
SELECT
a AS [blabla a],
b [blabla b],
CASE c
WHEN 1 THEN 'aaa'
WHEN 2 THEN 'bbb'
ELSE 'unknown'
END AS [my alias],
d AS [blabla d]
FROM mytable
Not in MySQL. I tried it and I get the following error:
ERROR 1054 (42S22): Unknown column 'a' in 'field list'
In MySql, alice name may not work, therefore put the original column name in the CASE statement
SELECT col1 as a, CASE WHEN col1 = 'test' THEN 'yes' END as value FROM table;
Sometimes above query also may return error, I don`t know why (I faced this problem in my two different development machine). Therefore put the CASE statement into the "(...)" as below:
SELECT col1 as a, (CASE WHEN col1 = 'test' THEN 'yes' END) as value FROM table;
Yes, you just need to add a parenthesis :
SELECT col1 as a, (CASE WHEN a = 'test' THEN 'yes' END) as value FROM table;
make it so easy.
select columnnameshow = (CASE tipoventa
when 'CONTADO' then 'contadito'
when 'CREDITO' then 'cred'
else 'no result'
end) from Promocion.Promocion