Use a case expression on resultset? - sql

I'm trying to write a SQL query with case and the conditions of the case is dependent on whether any records are found on a sub-query.
Select
Case When (Another Select statement which may return results) 'True'
Else 'False'
End As Has_Results
From TBL_ABC

You want an EXISTS condition in a CASE expression:
Select Case
When Exists (
Another Select statement which may return results
)
Then 'True'
Else 'False'
End As Has_Results
From TBL_ABC

You can use EXISTS:
SELECT
CASE
WHEN exists(other query) THEN 'True' --Subquery has record
ELSE 'False' --Subquery has not record
END;

Related

How to output 'is null' in a where clause in a case when statement?

The query checks the value of a parameter foo which is passed by a dropdown inside a program. If that paramater contains a certain value, an attribute should only contain null values. Can I manage that without pl/SQL?
select * from table t
where case when $foo$ = 'yes' then t.something is null end
Do you mean this logic?
select something
from table t
where ($foo$ = 'yes' and t.something is null) or ($foo != 'yes')
Just use nvl function :
select *
from mytable t
where nvl($foo$,'yes') = 'yes';

Oracle: CASE conditional STATEMENT in the WHERE Clause is not working?

I have below query with CASE statement and this is trowing "missing keyword error"
Can you please help.
select *
from podConfigKey_Tab PCK
WHERE
CASE WHEN (PCK.Keyid = 'TLMAPIConfigMgr.UseDB'
and PCK.DEFAULTKEYIDVALUE = 'FALSE')
THEN PCK.Keyid = 'TLMAPIConfigMgr.UseDB'
ELSE PCK.Keyid != 'TLMAPIConfigMgr.UseDB'
END;
A case expression returns a single value, not a syntactic construct like a=b. You could, however, emulate this behavior with a series of logical operators:
SELECT *
FROM podConfigKey_Tab PCK
WHERE PCK.DEFAULTKEYIDVALUE = 'FALSE' OR
PCK.Keyid != 'TLMAPIConfigMgr.UseDB'
your query should be something more like as mentioned below (removed the else portion to make the below query work), you need to have predicate after the WHERE clause so that you can match value that is return by the CASE statment
select * from podConfigKey_Tab PCK
WHERE PCK.Keyid =
CASE WHEN (PCK.Keyid = 'TLMAPIConfigMgr.UseDB' and PCK.DEFAULTKEYIDVALUE = 'FALSE') THEN 'TLMAPIConfigMgr.UseDB'
END ;
The Oracle CASE expression (like DECODE) returns a value, but by itself it is not a predicate which can evaluate to TRUE or FALSE. You need to establish some condition such that the value returned by the CASE statement can be evaluated. For example:
with sample_data as
(select 'dog' pet, 'y' has_fur from dual union all
select 'cat', 'y' from dual union all
select 'bird', 'n' from dual)
select *
from sample_data
where (case when has_fur = 'y' then 1 else 0 end) = 1;
SQL Fiddle Example

Is there a way to use the distinct keyword inside a conditional case statement of an SQL query?

I have an SQL statement in which I would like to use the distinct keyword inside a conditional case statement like so...
SELECT
case when <condition> then distinct t.myfield
else
null
end as my_field
If I try to run the query, though, I get a 'missing expression' error.
Any suggestions?
Thanks in advance
Distinct is for the entire select statement, not a single field. You could do the following:
SELECT DISTINCT Case when <condition> then t.myfield else null end as my_field
This will affect other fields in the select statement though. An alternative would be to add it to a separate query:
SELECT case when <condition> then t.myfield else null end as my_field
from (select distinct myfield from t) as t

How to check if a field exists in SQL subquery?

I have to do a lot of queries with this kind of logic:
Check if a table contains a record for a patient
If it does return then 'Yes'
Else return 'No'
Now, I want to create a procedure that will do this, so I tried to create a function that will do the above, but ended up in dynamic queries which is not possible in functions.
Is it possible to achieve this? How can I go about this?
PS:
Maybe something like:
select
(IF EXISTS(SELECT * FROM Dtl_Patient WHERE Pk = 3990 select 'Yes' else select 'No')) as output from dtl_AllPatient;
Try CASE
SELECT
CASE WHEN EXISTS (SELECT PatientID FROM Table2 T2 WHERE T2.PatientID =T1.PatientID)
THEN 'YES' ELSE 'NO' END AS PatientExists
FROM
Table1 T1
EDIT
SELECT
CASE WHEN EXISTS (SELECT Pk FROM Dtl_Patient WHERE Pk = 3990) THEN 'YES' ELSE 'NO' END AS PatientExists
FROM dtl_AllPatient
check this EXISTS Condition
The SQL EXISTS condition is considered "to be met" if the subquery returns at least one row.

SQL Query - Return Text based on numeric value

I am trying to work out a query where the query will perform a count (total) on a specific column. If the count is greater than 0, I want to display YES and display NO if the returned count is zero.
So, if I a query as this:
SELECT COUNT(ProblemID)
FROM dbo.ProblemInfo
WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')
that will actually be a subquery, how do I get the subquery to display "YES" when the returned count is greater than 0 and NO if the count is 0?
I appreciate any insight and help.
select isnull(
SELECT MAX('YES')
FROM dbo.ProblemInfo
WHERE ProblemID IN (100,101,309,305,205,600,500)
AND DEPID = '10866'),
'NO')
This is a trick to return either YES if there's at least one matching row, or null if not.
The wrapping isnull call then turns a null into a NO
Here's an alternate way of querying that.
IF EXISTS(
SELECT *
FROM dbo.ProblemInfo
WHERE (ProblemID IN (100,101,309,305,205,600,500))
AND (DEPID = '10866')
)
BEGIN
SELECT 'Yes'
END
ELSE
BEGIN
SELECT 'No'
END
What I like about this method is that, for enormous data-sets, it should be noticeably faster.
try
SELECT CASE
WHEN (SELECT COUNT(ProblemID) FROM dbo.ProblemInfo WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')) > 0
THEN 'YES'
ELSE 'NO' END
FROM YourTable
you can use case when.
SELECT
case
when COUNT(ProblemID) = 0 then 'NO'
else 'YES'
end
FROM dbo.ProblemInfo WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')