SQL case with different fields to check from same table - sql

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,

Related

How can I use CASE clause twice in SQL Query

I am trying to apply two conditions in one SQL Query.
(select DISTINCT (
CASE WHEN (
ABC.GemUserID = '99' )
OR ABC.GemUserID != '99'
THEN 'Yes'
ELSE 'No'
END)) AS AllWell
This gives me output as "Yes" where as the CASE is true only for 1 file like below :
Current Result:
99 , Yes
99 , Yes
99 , Yes
Expected Result:
99 , No
99 , No
99 , Yes
I am using the below query but the SQL Query Intellisence is identifying it as wrong.
Wrong Query:
(select DISTINCT (
CASE WHEN ( ABC.GEMUserID = '99' THEN 'Yes' else 'No'
CASE WHEN ( ABC.GEMUserID != '99' THEN 'No' else 'Yes'
END)) AS AllWell
After fixing the above Wrong Query:
(select DISTINCT
(CASE WHEN ABC.GemUserID = '99' THEN 'Yes' else 'No' END),
(CASE WHEN ABC.GemUserID != '99' THEN 'No' else 'Yes' END))
AS AllWell
But I am getting error:
Msg 116, Level 16, State 1, Line 17 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
How to fix this?
select distinct is -- itself -- part of SQL syntax. The distinct is not a function. It should not be followed by parentheses. So, if I understand your question:
select DISTINCT
( CASE WHEN ABC.GEMUserID = '99' THEN 'Yes' else 'No' END),
( CASE WHEN ABC.GEMUserID <> '99' THEN 'No' else 'Yes' END) as AllWell
Do you plan on giving the first column a name?
select DISTINCT
CASE WHEN ABC.GEMUserID = '99' THEN 'Yes'
ELSE 'No' -- This is automatically When ABC.GEMUserID <> '99'
END AS AllWell
According to the error, your query is a subquery (probably behind IN?) in a larger SQL command. Therefore, it is not possible for such subquery to return more than one column.
So your first query, you've said:
CASE WHEN userID = 99 OR userID != 99
In other words:
CASE WHEN 1=1
This is why it returns yes for everything (not sure what the difference between your current and expected result should be considering that the userID is 99 for all rows).
For your erroneous query, seems you're returning that select in the middle of another select (since you alias it at the end). Due to that, you cannot return more than one column in your nested select. You do not need the second CASE statement, simply change your query to:
(select DISTINCT
CASE WHEN ABC.GemUserID = '99' THEN 'Yes' Else 'No' End) AS AllWell
Assuming that you hold the missing pieces to the query such as the FROM.

Oracle SQL - Modify the Content Based on the Value From Another

I keep getting the ORA-00905: missing keyword error here. Could you take a look please?
The task is to replace the content of the attribute with 'N/A' if another variable is equal to 'R', otherwise - keep the value as is.
Here is an extract of my code:
SELECT
CASE
WHEN SCHEMA.TABLE.VAR1 = 'R' THEN SCHEMA.TABLE.VAR2 = 'N/A'
ELSE SCHEMA.TABLE.VAR2
END AS NEW_NAME
If you want a select, the column alias goes after the select. = is not allowed in the then clause:
SELECT (CASE WHEN SCHEMA.TABLE.VAR1 = 'R' THEN 'N/A'
ELSE SCHEMA.TABLE.VAR2
END) AS NEW_NAME
Note: You should use table aliases, so the query is easier to write and to read:
SELECT (CASE WHEN t.VAR1 = 'R' THEN 'N/A'
ELSE t.VAR2
END) AS NEW_NAME,
. . .
FROM SCHEMA.TABLE t . ..
You should be using an update here rather than a select, if you intend to change the data in your SQL table:
UPDATE yourTable
SET SCHEMA.TABLE.VAR2 = 'N/A'
WHERE SCHEMA.TABLE.VAR1 = 'R'
If you need to do a select here, then use the following:
SELECT
CASE WHEN SCHEMA.TABLE.VAR1 = 'R' THEN 'N/A' ELSE SCHEMA.TABLE.VAR2 END AS NEW_NAME
FROM yourTable

adding condition when case statement is true in postgresql

I have a query that where i need to put condition when case statement is true.I have tried like this but not geeting the correct value.
SELECT
name,count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15
FROM
companies companies
where
(CASE when no_answer='f' then value_s IS not NULL or value_n IS not NULL end )
SELECT name,
count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15
FROM
companies companies
where
CASE when no_answer='f' then value_s ELSE '1' end IS not NULL
OR CASE when no_answer='f' then value_n ELSE '1' end IS not NULL
CASE is an expression, you can only specify a value after the THEN part, not a condition like you did THEN value_s IS NOT NULL
You can't use case like that.
It's hard to figure out what you are trying to do, but test the result of the case, rather than putting the test inside the case, like this:
...
where CASE when no_answer='f' then value_s else value_n end
IS not NULL

Shorter CASE ISNULL(qry, '')='' THEN 'no' ELSE 'yes' END

Is there a shorter/better way to write this SQL statement?
Edit: the inner sql select statement is a placeholder, the actual statement is more complex.
SELECT
CASE WHEN
ISNULL((SELECT TOP 1 x FROM y), '') = ''
THEN 'No'
ELSE 'Yes'
END AS BooleanValue
It feels very kludgey because it compares the result of the select statement to null, then sets to an empty string if null; just to check if it is an empty string, and set it to what it actually needs to be: a 'yes' or 'no' string.
Here's one way to do it a bit cleaner.
SELECT
CASE WHEN
(SELECT TOP 1 x FROM y) IS NULL
THEN 'No'
ELSE 'Yes'
END AS BooleanValue
This removes one extra command and should have the same output. I just tested it in sql server 2012.
SELECT ISNULL((SELECT TOP 1 'Yes' FROM x), 'No') as Boolean

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]