Use Case and if to check values - sql

I am trying to do some calculated column, my scenario is like the below example.
e.g: Whenever col1 is 'Yes', check Col2, if it has any value then calculated column will be 'True'.
Whenever Col1 is 'No', then calculated column will be 'Unknown'.
Whenever col1 is 'Yes', check Col2, if it is null then calculated column will be 'False'.
Col1 Col2 CalculatedColumn
Yes 123 True
No null Unknown
yes null False
Any help how can I achieve that?

You would use case:
select (case when col1 = 'Yes' and col2 is not null then 'True'
when col1 = 'No' then 'Unknown'
when col1 = 'Yes' and col2 is null then 'False'
end) as calculated_column
This can be simplified to:
select (case when col1 = 'Yes' and col2 is not null then 'True'
when col1 = 'Yes' then 'False'
else 'Unknown'
end) as calculated_column

You can use CASE..WHEN clause to meet this requirement. Converting your requirements in CASE..WHEN clause -
case when col1 = 'Yes' and col2 is not null then 'True'
when col1 = 'Yes' and col2 is null then 'False'
when Col1 = 'No' or col1 is null then 'Unknown' end as calculated_col

Related

Comparing two columns in the same table

I have two columns and I want to compare the values in each row and create a third column that will tell me true or false (or 0/1) like the below example.
Col1 Col2 Col3
24 24 true
45 45 true
56 54 false
78 98 false
Personally a CASE expression is probably the easiest and simplest way to do this.
SELECT col1,
col2,
CASE
WHEN col1 = col2 THEN 'True'
ELSE 'False'
END AS Is_A_Match
This should work for you:
select col1, col2, case when col1 = col2 then 'true' else 'false' end as col3
If you want to add a column into the table, then you can use a computed column:
alter table t add col3 as (case when col2 = col3 then 1 else 0 end)
If the columns can have NULL values and you want to treat those as equal, then the logic should take this into account.
This column is then available when referring to the table.
If you just want the result in a query, use a case expression as described in several other answers.
Simply use a case expression.
case when col1 = col2 then 'true' else 'false'
or in bit form
case when col1 = col2 then 1 else 0

Output of Multiple Select Statement In Single Row

I have multiple query which is providing count based on different where condition.
Will it be possible to bring all result in single Rows.
for eg:
Query1:
SELECT COUNT(COL25) ASSURED, FROM TAB1 WHERE COL1= 'ALPHA' AND COL2='ROLE'
Query2:
SELECT COUNT(COL25) RELEASE FROM TAB1 WHERE COL3 in('BEETA','X','Y') AND COLSTATUS='ABC'
The result for 1st query is ASSURED = 100
and 2nd Query is RELEASE = 5000
i am trying to display output as
ASSURED | RELEASE
100 | 5000
Use conditional aggregation:
SELECT SUM(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' THEN 1 ELSE 0 END) as ASSURED,
SUM(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' THEN 1 ELSE 0 END) as RELEASE
FROM TAB1;
EDIT:
If you actually need to count non-NULL values, you can be explicit (my preference):
SELECT SUM(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' AND col25 IS NOT NULL THEN 1 ELSE 0 END) as ASSURED,
SUM(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' AND col25 IS NOT NULL THEN 1 ELSE 0 END) as RELEASE
FROM TAB1;
Or be a bit more implicit:
SELECT COUNT(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' THEN col25 END) as ASSURED,
COUNT(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' THEN col25 END) as RELEASE
FROM TAB1;
You can also use joins, but #Gordon Linoff answer is cleaner and way shorter than this.
SELECT first.ASSURED, second.RELEASE
FROM
(SELECT
COUNT(COL25) ASSURED
FROM TAB1
WHERE COL1= 'ALPHA' AND COL2='ROLE') AS first
INNER JOIN (SELECT
COUNT(COL25) RELEASE
FROM TAB1
WHERE COL3 in('BEETA','X','Y')
AND COLSTATUS='ABC') AS second
ON 1=1

Nested Case for hard coding result of a case statement

I have a dataset similar to this;
col1 col2 col3
1 YES NO
2 NO YES
I am trying to apply case statement,
case when col1 = 1 then col2
when col1 = 2 then col3 end as newcol
Now the newcol will have values as YES/NO in the output. Is it possible to apply another case inside the above case condition, So that I can hardcode YES as Y and NO as N.
I have got the result by adding a case statement in the an outer query. Is there any alternate approach like nested case.
Also can I apply case condition using the column alias newcol?
You can add a case expression around your case, like this:
case (
case
when col1 = 1 then col2
when col1 = 2 then col3
end
) when 'YES' then 'Y' else 'N' end as newcol
SUBSTR would pick the first character of YES or NO without a conditional:
SUBSTR(
case
when col1 = 1 then col2
when col1 = 2 then col3
end
, 1
, 1
) as newcol

Retrieve single result with 2 columns in oracle

Do we have any function in oracle do this scenario.
There are 2 columns in table col1, col2.
col1 either has 'Y' or 'N' or NULL
col2 eitehr has 'Y' or 'N' or NULL
But always only one of the column has value 'Y'.
So , i want to check first col1 whether it has 'Y' ,if it has i want a string "COL1 found" ,if col2 has 'Y' i want a string "COL2 found". Is it possible with NVL2 and Decode function. With one column ,i can able to do that. But here i have to check 2 columns .Please note that i want a result in single row.
Regards,
Chaitu
select case when col1 = 'Y' then 'Col1 found' when col2 = 'Y' then 'Col2 found' end f
from yourtable
UPD.
select decode (col1, 'Y', 'col1 found', decode(col2, 'Y', 'col2 found')) from yourtable

Using "like" inside a "case" statement with 2 different fields

I have 2 fields in my table on which i need a case statement.
Col1 Col2 Col3
abc.txt Y 0
def.txt N 0
bbck.txt Y 1
The Col3 values are based on the Col1 and Col2 values in the following manner.
Y = 1 and N = 0. So all the values in Col2 that are Y shall become 1 in col3, and Nin Col2 will become 0 in Col3, UNLESS the col1 value ends with %c.txt. As you can see since the abc.txt ends with %c.txt the value in col3 becomes 0.
I know this can be done with a CASE statement nested maybe to get this done. Does anyone know how to?
here's my code
SELECT
CASE Col2
WHEN 'Y' THEN '1'
WHEN 'N' THEN '0'
ELSE
(CASE WHEN [Col1] LIKE '%c.txt' THEN '0'
END)
END
AS Col3,
*
FROM Tabl1
Hope this gives an idea
Maybe:
SELECT
CASE
WHEN Col2 = 'N'
OR Col1 LIKE '%c.txt'
THEN '0'
WHEN Col2 = 'Y'
THEN '1'
END AS Col3
, *
FROM Tabl1
SELECT
CASE WHEN Col2 LIKE '%c.txt' THEN '0'
ELSE
CASE
WHEN Col2 = 'N' THEN '0'
WHEN Col2 = 'Y' THEN '1'
END
END AS COL3
FROM Tabl1
I don't think your spec is clear enough e.g. is the comma before the word "UNLESS" suposed to incidicate that the following clause applies to both the previous clauses?
There seems to be four possible combinations:
1) Col1 LIKE '%c.txt'
AND Col2 = 'Y'
2) Col1 LIKE '%c.txt'
AND Col2 = 'N'
3) Col1 NOT LIKE '%c.txt'
AND Col2 = 'Y'
4) Col1 NOT LIKE '%c.txt'
AND Col2 = 'N'
You say
i need a case statement
Is this true? With SQL, the same thing can always be achieved multiple ways (which is why it is so hard to build a good optimizer :) I think a spec should state what is required and not place unreasonable restrictions on how to implement it.
The following uses UNION rather than case and if you had considered this from the start then maybe your spec would be better ;)
WITH Tabl1
AS
(
SELECT *
FROM (
VALUES ('abc.txt', 'Y'),
('def.txt', 'N'),
('bbck.txt', 'Y'),
('disc.txt', 'N')
) AS T (Col1, Col2)
)
SELECT '0' AS Col3, *
FROM Tabl1
WHERE Col1 LIKE '%c.txt'
AND Col2 = 'Y'
UNION
SELECT '0' AS Col3, *
FROM Tabl1
WHERE Col1 LIKE '%c.txt'
AND Col2 = 'N'
UNION
SELECT '1' AS Col3, *
FROM Tabl1
WHERE Col1 NOT LIKE '%c.txt'
AND Col2 = 'Y'
UNION
SELECT '0' AS Col3, *
FROM Tabl1
WHERE Col1 NOT LIKE '%c.txt'
AND Col2 = 'N';
P.S. I coded this to match the accepted answer but I'm not convinced it is correct!
This might be what your looking for:
SELECT * FROM Tabl1
cross apply
(
select Col3 =
CASE
WHEN Tabl1.Col2 = 'Y' then '1'
WHEN Tabl1.Col2 = 'Y' then '1'
WHEN RIGHT(Tabl1.Col1, 6) = 'c.txt' then '0'
END
) as Col3